"use client";

import React, { useEffect, useState } from "react";
import { Package, CalendarCheck, Clock, TrendingUp, ShoppingBag, Users, AlertTriangle, Euro } from "lucide-react";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Badge } from "@/components/ui/badge";
import { useAdmin } from "@/contexts/AdminContext";
import { useLanguage } from "@/contexts/LanguageContext";
import { getDashboardStats, DashboardStats } from "@/lib/api/admin";

export default function AdminOverview() {
  const { reservations } = useAdmin();
  const { t, locale } = useLanguage();
  const [stats, setStats] = useState<DashboardStats | null>(null);

  useEffect(() => {
    getDashboardStats().then(setStats).catch((e) => console.error("[admin] getDashboardStats failed:", e));
  }, []);

  const upcoming = [...reservations]
    .filter((r) => r.status !== "cancelled")
    .sort((a, b) => `${a.date}${a.time}`.localeCompare(`${b.date}${b.time}`))
    .slice(0, 5);

  const mainCards = stats
    ? [
        { label: t("admin.overview.orders_pending"),    value: stats.orders_pending,    icon: ShoppingBag, color: "text-orange-600", bg: "bg-orange-100" },
        { label: t("admin.overview.bookings_pending"),  value: stats.bookings_pending,  icon: Clock,       color: "text-amber-600",  bg: "bg-amber-100" },
        { label: t("admin.overview.bookings_confirmed"),value: stats.bookings_confirmed,icon: CalendarCheck,color: "text-green-600", bg: "bg-green-100" },
        { label: t("admin.overview.products_count"),    value: stats.products_count,    icon: Package,     color: "text-blue-600",   bg: "bg-blue-100" },
      ]
    : [];

  const revenueCards = stats
    ? [
        { label: t("admin.overview.revenue_today"),  value: `${stats.revenue_today.toFixed(2)} €`,  icon: Euro,       color: "text-emerald-600", bg: "bg-emerald-100" },
        { label: t("admin.overview.revenue_week"),   value: `${stats.revenue_week.toFixed(2)} €`,   icon: TrendingUp, color: "text-emerald-600", bg: "bg-emerald-100" },
        { label: t("admin.overview.revenue_month"),  value: `${stats.revenue_month.toFixed(2)} €`,  icon: TrendingUp, color: "text-primary",     bg: "bg-primary/10" },
        { label: t("admin.overview.new_customers"),  value: stats.new_customers_month,               icon: Users,      color: "text-purple-600",  bg: "bg-purple-100" },
      ]
    : [];

  const Skeleton = () => (
    <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4">
      {Array.from({ length: 4 }).map((_, i) => (
        <Card key={i}>
          <CardContent className="p-5">
            <div className="h-4 bg-muted animate-pulse rounded w-2/3 mb-3" />
            <div className="h-7 bg-muted animate-pulse rounded w-1/3" />
          </CardContent>
        </Card>
      ))}
    </div>
  );

  const StatGrid = ({ cards }: { cards: { label: string; value: string | number; icon: React.ElementType; color: string; bg: string }[] }) => (
    <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4">
      {cards.map((s) => (
        <Card key={s.label}>
          <CardContent className="p-5 flex items-center justify-between">
            <div>
              <p className="text-sm text-muted-foreground">{s.label}</p>
              <p className="text-2xl font-semibold mt-1">{s.value}</p>
            </div>
            <div className={`h-10 w-10 rounded-full ${s.bg} flex items-center justify-center`}>
              <s.icon className={`h-5 w-5 ${s.color}`} />
            </div>
          </CardContent>
        </Card>
      ))}
    </div>
  );

  return (
    <div className="space-y-8">
      <div>
        <h2 className="font-serif text-2xl font-semibold">{t("admin.overview.title")}</h2>
        <p className="text-sm text-muted-foreground mt-1">{t("admin.overview.subtitle")}</p>
      </div>

      <div className="space-y-2">
        <h3 className="text-sm font-medium text-muted-foreground uppercase tracking-wide">{t("admin.overview.activity_section")}</h3>
        {stats === null ? <Skeleton /> : <StatGrid cards={mainCards} />}
      </div>

      <div className="space-y-2">
        <h3 className="text-sm font-medium text-muted-foreground uppercase tracking-wide">{t("admin.overview.revenue_section")}</h3>
        {stats === null ? <Skeleton /> : <StatGrid cards={revenueCards} />}
      </div>

      {stats && stats.low_stock_count > 0 && (
        <Card className="border-amber-200 bg-amber-50">
          <CardContent className="p-4 flex items-center gap-3">
            <AlertTriangle className="h-5 w-5 text-amber-600 flex-shrink-0" />
            <p className="text-sm text-amber-800">
              {t("admin.overview.low_stock", { n: stats.low_stock_count })}
            </p>
          </CardContent>
        </Card>
      )}

      <Card>
        <CardHeader>
          <CardTitle className="text-base">{t("admin.overview.upcoming_title")}</CardTitle>
        </CardHeader>
        <CardContent>
          {upcoming.length === 0 ? (
            <p className="text-sm text-muted-foreground">{t("admin.overview.no_upcoming")}</p>
          ) : (
            <div className="space-y-3">
              {upcoming.map((r) => (
                <div key={r.id} className="flex items-center justify-between py-3 border-b border-border last:border-0">
                  <div>
                    <p className="font-medium text-sm">{r.clientName}</p>
                    <p className="text-xs text-muted-foreground">{r.service}</p>
                  </div>
                  <div className="text-right">
                    <p className="text-sm font-medium">
                      {new Date(r.date).toLocaleDateString(locale, { day: "2-digit", month: "short" })} à {r.time}
                    </p>
                    <Badge variant={r.status === "confirmed" ? "default" : "secondary"} className="mt-1 text-xs">
                      {r.status === "confirmed" ? t("admin.status.confirmed") : t("admin.status.pending")}
                    </Badge>
                  </div>
                </div>
              ))}
            </div>
          )}
        </CardContent>
      </Card>
    </div>
  );
}
