"use client";

import { TrendingUp, TrendingDown, Eye, Users, Clock, FileText } from "lucide-react";

interface MetricsData {
    totalApplications: number;
    unreviewed: number;
    activeCandidates: number;
    avgTimeToHire: number | null;
    weeklyApps: number;
    monthlyApps: number;
    recentUnreviewed24h: number;
    pipelineBreakdown: {
        screening: number;
        interviewing: number;
        finalReview: number;
    };
}

interface MetricsCardsProps {
    metrics: MetricsData;
}

export function MetricsCards({ metrics }: MetricsCardsProps) {
    const cards = [
        {
            title: "Total Applications",
            value: metrics.totalApplications,
            subtitle: `${metrics.weeklyApps} this week, ${metrics.monthlyApps} this month`,
            icon: FileText,
            color: "from-blue-500 to-blue-600",
            bgLight: "bg-blue-50",
            textColor: "text-blue-700",
            borderColor: "border-blue-100",
        },
        {
            title: "Unreviewed",
            value: metrics.unreviewed,
            subtitle: metrics.recentUnreviewed24h > 0
                ? `${metrics.recentUnreviewed24h} received in last 24h`
                : "All caught up!",
            icon: Eye,
            color: "from-orange-500 to-amber-500",
            bgLight: "bg-orange-50",
            textColor: "text-orange-700",
            borderColor: "border-orange-100",
            urgent: metrics.recentUnreviewed24h > 5,
        },
        {
            title: "Active Candidates",
            value: metrics.activeCandidates,
            subtitle: `${metrics.pipelineBreakdown.screening} screening, ${metrics.pipelineBreakdown.interviewing} interviewing, ${metrics.pipelineBreakdown.finalReview} final review`,
            icon: Users,
            color: "from-emerald-500 to-green-500",
            bgLight: "bg-emerald-50",
            textColor: "text-emerald-700",
            borderColor: "border-emerald-100",
        },
        {
            title: "Time to Hire",
            value: metrics.avgTimeToHire !== null ? `${metrics.avgTimeToHire}d` : "—",
            subtitle: metrics.avgTimeToHire !== null
                ? (metrics.avgTimeToHire <= 23 ? "Better than industry avg (23d)" : "Industry avg: 23 days")
                : "No hires yet",
            icon: Clock,
            color: "from-violet-500 to-purple-600",
            bgLight: "bg-violet-50",
            textColor: "text-violet-700",
            borderColor: "border-violet-100",
            trend: metrics.avgTimeToHire !== null
                ? (metrics.avgTimeToHire <= 23 ? "positive" : "negative")
                : null,
        },
    ];

    return (
        <div className="grid grid-cols-1 sm:grid-cols-2 xl:grid-cols-4 gap-4">
            {cards.map((card) => {
                const Icon = card.icon;
                return (
                    <div
                        key={card.title}
                        className={`relative overflow-hidden rounded-2xl border ${card.borderColor} bg-white p-5 shadow-sm hover:shadow-md transition-all duration-300 group`}
                    >
                        {/* Gradient accent line at top */}
                        <div
                            className={`absolute top-0 left-0 right-0 h-1 bg-gradient-to-r ${card.color} opacity-80`}
                        />

                        <div className="flex items-start justify-between mb-3">
                            <div
                                className={`flex items-center justify-center w-10 h-10 rounded-xl ${card.bgLight} group-hover:scale-110 transition-transform duration-300`}
                            >
                                <Icon className={`w-5 h-5 ${card.textColor}`} />
                            </div>
                            {card.trend && (
                                <span
                                    className={`flex items-center gap-1 text-xs font-bold px-2 py-1 rounded-full ${card.trend === "positive"
                                        ? "bg-emerald-50 text-emerald-600"
                                        : "bg-red-50 text-red-600"
                                        }`}
                                >
                                    {card.trend === "positive" ? (
                                        <TrendingDown className="w-3 h-3" />
                                    ) : (
                                        <TrendingUp className="w-3 h-3" />
                                    )}
                                    {card.trend === "positive" ? "Better" : "Slower"}
                                </span>
                            )}
                            {(card as any).urgent && (
                                <span className="flex items-center gap-1 text-xs font-bold px-2 py-1 rounded-full bg-orange-100 text-orange-700 animate-pulse">
                                    Action needed
                                </span>
                            )}
                        </div>

                        <p className="text-sm font-medium text-slate-500 mb-1">{card.title}</p>
                        <p className="text-3xl font-extrabold text-slate-900 tracking-tight">
                            {typeof card.value === "number" ? card.value.toLocaleString() : card.value}
                        </p>
                        <p className="text-xs text-slate-400 mt-2 leading-relaxed">{card.subtitle}</p>
                    </div>
                );
            })}
        </div>
    );
}
