"use client";

import {
    Video, Phone, MapPin, Code2, Users, Award,
    TrendingUp, Calendar, Clock, Star, BarChart3,
    CheckCircle2, XCircle, AlertCircle
} from "lucide-react";

interface AnalyticsData {
    total: number;
    scheduled: number;
    completed: number;
    cancelled: number;
    byType: Record<string, number>;
    noShowRate: number;
    confirmationRate: number;
    avgDurationMinutes: number;
    avgScore: number | null;
    scorecardCount: number;
}

const typeIcons: Record<string, { icon: any; label: string; color: string }> = {
    phone_screen: { icon: Phone, label: "Phone Screen", color: "text-emerald-600 bg-emerald-50" },
    video: { icon: Video, label: "Video", color: "text-blue-600 bg-blue-50" },
    in_person: { icon: MapPin, label: "In-Person", color: "text-violet-600 bg-violet-50" },
    technical: { icon: Code2, label: "Technical", color: "text-orange-600 bg-orange-50" },
    panel: { icon: Users, label: "Panel", color: "text-pink-600 bg-pink-50" },
    final: { icon: Award, label: "Final", color: "text-amber-600 bg-amber-50" },
};

export function InterviewAnalytics({ data }: { data: AnalyticsData | null }) {
    if (!data || data.total === 0) {
        return (
            <div className="text-center py-12 bg-slate-50/50 border-2 border-dashed border-slate-200 rounded-2xl">
                <BarChart3 className="w-10 h-10 mx-auto mb-3 text-slate-300" />
                <h4 className="text-base font-bold text-slate-700">No Interview Data Yet</h4>
                <p className="text-sm text-slate-500 mt-1">
                    Start scheduling interviews to see analytics here.
                </p>
            </div>
        );
    }

    const completionRate = data.total > 0 ? Math.round((data.completed / data.total) * 100) : 0;

    return (
        <div className="space-y-5">
            <div>
                <h3 className="text-lg font-bold text-slate-900 flex items-center gap-2">
                    <BarChart3 className="w-5 h-5 text-primary" />
                    Interview Analytics
                </h3>
                <p className="text-sm text-slate-500 mt-0.5">
                    Performance overview across all interviews
                </p>
            </div>

            {/* Top Metrics */}
            <div className="grid grid-cols-2 md:grid-cols-4 gap-3">
                <MetricCard
                    icon={Calendar}
                    label="Total Interviews"
                    value={data.total}
                    color="text-primary"
                    bgColor="bg-primary/5"
                />
                <MetricCard
                    icon={CheckCircle2}
                    label="Completion Rate"
                    value={`${completionRate}%`}
                    color="text-emerald-600"
                    bgColor="bg-emerald-50"
                />
                <MetricCard
                    icon={XCircle}
                    label="Cancel / No-Show"
                    value={`${data.noShowRate}%`}
                    color="text-red-600"
                    bgColor="bg-red-50"
                />
                <MetricCard
                    icon={AlertCircle}
                    label="Confirmation Rate"
                    value={`${data.confirmationRate}%`}
                    color="text-amber-600"
                    bgColor="bg-amber-50"
                />
            </div>

            {/* Second Row */}
            <div className="grid grid-cols-2 md:grid-cols-3 gap-3">
                <MetricCard
                    icon={Clock}
                    label="Avg Duration"
                    value={`${data.avgDurationMinutes}min`}
                    color="text-blue-600"
                    bgColor="bg-blue-50"
                />
                <MetricCard
                    icon={Star}
                    label="Avg Score"
                    value={data.avgScore ? `${data.avgScore}/5` : "—"}
                    color="text-amber-600"
                    bgColor="bg-amber-50"
                />
                <MetricCard
                    icon={TrendingUp}
                    label="Scorecards"
                    value={data.scorecardCount}
                    color="text-violet-600"
                    bgColor="bg-violet-50"
                />
            </div>

            {/* Interview by Type */}
            <div className="bg-white border border-slate-200 rounded-xl p-5">
                <p className="text-xs font-bold text-slate-500 uppercase tracking-wide mb-3">
                    Interviews by Type
                </p>
                <div className="space-y-3">
                    {Object.entries(data.byType)
                        .sort((a, b) => b[1] - a[1])
                        .map(([type, count]) => {
                            const tc = typeIcons[type] || typeIcons.video;
                            const Icon = tc.icon;
                            const pct = data.total > 0 ? Math.round((count / data.total) * 100) : 0;
                            return (
                                <div key={type} className="flex items-center gap-3">
                                    <div className={`w-8 h-8 rounded-lg flex items-center justify-center ${tc.color}`}>
                                        <Icon className="w-4 h-4" />
                                    </div>
                                    <span className="text-sm font-semibold text-slate-700 w-28">
                                        {tc.label}
                                    </span>
                                    <div className="flex-1 h-3 bg-slate-100 rounded-full overflow-hidden">
                                        <div
                                            className="h-full bg-gradient-to-r from-primary to-indigo-500 rounded-full transition-all"
                                            style={{ width: `${pct}%` }}
                                        />
                                    </div>
                                    <span className="text-sm font-bold text-slate-900 w-12 text-right">
                                        {count}
                                    </span>
                                    <span className="text-xs text-slate-400 w-10 text-right">
                                        {pct}%
                                    </span>
                                </div>
                            );
                        })}
                </div>
            </div>

            {/* Status Breakdown */}
            <div className="grid grid-cols-3 gap-3">
                <StatusPill label="Scheduled" count={data.scheduled} color="bg-blue-100 text-blue-800" />
                <StatusPill label="Completed" count={data.completed} color="bg-emerald-100 text-emerald-800" />
                <StatusPill label="Cancelled" count={data.cancelled} color="bg-red-100 text-red-800" />
            </div>
        </div>
    );
}

function MetricCard({
    icon: Icon,
    label,
    value,
    color,
    bgColor,
}: {
    icon: any;
    label: string;
    value: string | number;
    color: string;
    bgColor: string;
}) {
    return (
        <div className={`p-4 rounded-xl ${bgColor} border border-slate-100`}>
            <Icon className={`w-5 h-5 ${color} mb-2`} />
            <p className="text-2xl font-black text-slate-900">{value}</p>
            <p className="text-xs font-semibold text-slate-500 mt-0.5">{label}</p>
        </div>
    );
}

function StatusPill({ label, count, color }: { label: string; count: number; color: string }) {
    return (
        <div className="text-center p-3 bg-white border border-slate-100 rounded-xl">
            <span className={`inline-block px-2 py-0.5 text-xs font-bold rounded-full ${color} mb-1`}>
                {label}
            </span>
            <p className="text-lg font-black text-slate-900">{count}</p>
        </div>
    );
}
