"use client";

import { MoreHorizontal, TrendingUp, TrendingDown, Clock, CheckCircle2, XCircle, Calendar as CalendarIcon, FileText, Users, UserCheck, CheckCircle, XOctagon, Inbox } from "lucide-react";
import Link from "next/link";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Badge } from "@/components/ui/badge";
import { Progress } from "@/components/ui/progress";
import { Button } from "@/components/ui/button";

interface StatsCardsProps {
    applications: any[];
}

export function StatsCards({ applications }: StatsCardsProps) {
    // Calculate real-time statistics
    const totalApplications = applications.length;
    const pendingCount = applications.filter(a => a.status === 'pending').length;
    const reviewingCount = applications.filter(a => a.status === 'reviewing').length;
    const interviewCount = applications.filter(a => a.status === 'interview').length;
    const acceptedCount = applications.filter(a => ["accepted", "offered"].includes(a.status)).length;
    const rejectedCount = applications.filter(a => a.status === 'rejected').length;

    return (
        <div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-6">
            <StatCard
                label="Total Apps"
                value={totalApplications.toString()}
                icon={<FileText className="h-5 w-5" />}
                color="blue"
                href="/dashboard/applications"
            />
            <StatCard
                label="Pending"
                value={pendingCount.toString()}
                icon={<Clock className="h-5 w-5" />}
                color="yellow"
                href="/dashboard/applications?status=pending"
            />
            <StatCard
                label="Shortlisted"
                value={reviewingCount.toString()}
                icon={<Users className="h-5 w-5" />}
                color="indigo"
                href="/dashboard/applications?status=reviewing"
            />
            <StatCard
                label="Interviewing"
                value={interviewCount.toString()}
                icon={<UserCheck className="h-5 w-5" />}
                color="purple"
                href="/dashboard/applications?status=interview"
            />
            <StatCard
                label="Offered"
                value={acceptedCount.toString()}
                icon={<CheckCircle className="h-5 w-5" />}
                color="emerald"
                href="/dashboard/applications?status=accepted"
            />
            <StatCard
                label="Rejected"
                value={rejectedCount.toString()}
                icon={<XOctagon className="h-5 w-5" />}
                color="rose"
                href="/dashboard/applications?status=rejected"
            />
        </div>
    );
}

function StatCard({ label, value, icon, color, href }: { label: string; value: string; icon: React.ReactNode; color: string; href: string }) {
    const colorClasses: Record<string, string> = {
        blue: "bg-blue-50 text-blue-600 ring-blue-100",
        indigo: "bg-indigo-50 text-indigo-600 ring-indigo-100",
        purple: "bg-purple-50 text-purple-600 ring-purple-100",
        emerald: "bg-emerald-50 text-emerald-600 ring-emerald-100",
        rose: "bg-rose-50 text-rose-600 ring-rose-100",
        yellow: "bg-amber-50 text-amber-600 ring-amber-100",
    };

    return (
        <Link href={href}>
            <Card className="flex flex-col justify-between transition-all hover:border-primary/40 hover:shadow-md group h-full">
                <CardContent className="p-5">
                    <div className="flex items-start justify-between mb-4">
                        <div className={`rounded-xl p-2.5 ring-1 ring-inset ${colorClasses[color]}`}>
                            {icon}
                        </div>
                    </div>
                    <div>
                        <span className="block text-2xl font-black tracking-tight text-slate-900 group-hover:text-primary transition-colors">{value}</span>
                        <span className="mt-1 block text-xs font-semibold uppercase tracking-wider text-slate-500">{label}</span>
                    </div>
                    <div className="mt-4 pt-3 border-t border-slate-100">
                        <span className="text-xs font-bold text-primary opacity-0 -translate-x-2 group-hover:opacity-100 group-hover:translate-x-0 transition-all flex items-center gap-1">
                            View Details <span className="text-[10px] leading-none">→</span>
                        </span>
                    </div>
                </CardContent>
            </Card>
        </Link>
    );
}

export function TaskProgressWidget() {
    return (
        <Card>
            <CardHeader className="flex flex-row items-center justify-between pb-2">
                <CardTitle className="text-lg flex items-center gap-2">
                    <CheckCircle2 className="h-5 w-5 text-indigo-500" /> Tasks
                </CardTitle>
            </CardHeader>
            <CardContent>
                <div className="flex flex-col items-center justify-center py-8 text-center">
                    <CheckCircle2 className="h-10 w-10 text-slate-300 mb-3" />
                    <p className="font-semibold text-slate-700 mb-1">Task Management</p>
                    <p className="text-sm text-slate-500 mb-4">Recruitment tasks and workflows will be available in a future update.</p>
                    <Badge variant="secondary">Coming Soon</Badge>
                </div>
            </CardContent>
        </Card>
    );
}

export function ApplicantSourcesWidget() {
    return (
        <Card>
            <CardHeader className="flex flex-row items-center justify-between pb-2">
                <CardTitle className="text-lg flex items-center gap-2">
                    <TrendingUp className="h-5 w-5 text-emerald-500" /> Sourcing
                </CardTitle>
            </CardHeader>
            <CardContent>
                <div className="flex flex-col items-center justify-center py-8 text-center">
                    <TrendingUp className="h-10 w-10 text-slate-300 mb-3" />
                    <p className="font-semibold text-slate-700 mb-1">Source Analytics</p>
                    <p className="text-sm text-slate-500 mb-4">Track where your best candidates come from. Analytics will activate once you have incoming applications.</p>
                    <Badge variant="secondary">Coming Soon</Badge>
                </div>
            </CardContent>
        </Card>
    );
}

export function ScheduleWidget() {
    return (
        <Card>
            <CardHeader className="flex flex-row items-center justify-between pb-2">
                <CardTitle className="text-lg flex items-center gap-2">
                    <CalendarIcon className="h-5 w-5 text-blue-500" /> Schedule
                </CardTitle>
                <Link href="/dashboard/calendar" className="text-sm font-semibold text-primary hover:underline">
                    View full
                </Link>
            </CardHeader>
            <CardContent>
                <div className="flex flex-col items-center justify-center py-8 text-center">
                    <CalendarIcon className="h-10 w-10 text-slate-300 mb-3" />
                    <p className="font-semibold text-slate-700 mb-1">No upcoming events</p>
                    <p className="text-sm text-slate-500">Scheduled interviews and meetings will appear here.</p>
                </div>
            </CardContent>
        </Card>
    );
}
