'use client'

import { Wallet, Users, CreditCard, Clock } from 'lucide-react'
import { usePayments } from '@/contexts/PaymentsContext'
import { formatMoney } from '@/lib/utils'
import { Card, CardContent } from '@/components/ui/card'
import { Skeleton } from '@/components/ui/skeleton'
import { cn } from '@/lib/utils'

const kpiConfig = [
  { title: 'Revenu total',    icon: Wallet,     color: 'text-emerald-600', bg: 'bg-emerald-50', sub: 'Tous les paiements'   },
  { title: 'Transactions',    icon: CreditCard,  color: 'text-blue-600',    bg: 'bg-blue-50',    sub: 'Paiements effectués'  },
  { title: 'Clients uniques', icon: Users,       color: 'text-violet-600',  bg: 'bg-violet-50',  sub: 'Clients ayant payé'   },
  { title: 'Temps moyen',     icon: Clock,       color: 'text-amber-600',   bg: 'bg-amber-50',   sub: 'De traitement'        },
]

export function PaymentDashboard() {
  const { stats, loading } = usePayments()

  const values = stats
    ? [
        formatMoney(stats.totalRevenue),
        stats.transactionCount.toLocaleString('fr-FR'),
        stats.uniqueClients.toLocaleString('fr-FR'),
        `${stats.avgProcessingTime.toFixed(1)}s`,
      ]
    : []

  return (
    <div className="grid grid-cols-1 sm:grid-cols-2 xl:grid-cols-4 gap-4">
      {kpiConfig.map((kpi, i) => (
        <Card key={kpi.title} className="border border-gray-200 shadow-none bg-white">
          <CardContent className="p-5">
            <div className={cn('w-8 h-8 rounded-lg flex items-center justify-center mb-3', kpi.bg)}>
              <kpi.icon className={cn('w-4 h-4', kpi.color)} />
            </div>
            {loading ? (
              <div className="space-y-2">
                <Skeleton className="h-7 w-24" />
                <Skeleton className="h-3 w-20" />
              </div>
            ) : (
              <>
                <p className="text-2xl font-bold text-gray-900 tracking-tight tabular-nums">
                  {values[i] ?? '—'}
                </p>
                <p className="text-xs font-medium text-gray-400 mt-1">{kpi.sub}</p>
              </>
            )}
          </CardContent>
        </Card>
      ))}
    </div>
  )
}
