import { getCurrentUser } from "@/lib/auth";
import { redirect } from "next/navigation";
import Link from "next/link";
import {
    Kanban, GitBranch, Users, ClipboardList,
    ArrowRight, BarChart3, CheckCircle2, Clock, Sparkles
} from "lucide-react";
import { getEmployerDashboardMetrics, getEmployerJobsList } from "@/app/actions/employerDashboard";

export default async function ATSPage() {
    const user = await getCurrentUser();

    if (!user) redirect("/login");
    if (user.role !== "employer") redirect("/dashboard");

    const [metrics, jobs] = await Promise.all([
        getEmployerDashboardMetrics(),
        getEmployerJobsList(),
    ]);

    const activeJobs = (jobs as any[]).filter((j: any) => j.status === "published").length;

    return (
        <div className="space-y-8">
            {/* Header */}
            <div>
                <h1 className="text-3xl font-extrabold text-slate-900">Applicant Tracking System</h1>
                <p className="text-slate-500 mt-1">
                    Manage your hiring pipelines, track candidates, and collaborate with your team.
                </p>
            </div>

            {/* Quick Stats */}
            <div className="grid grid-cols-2 sm:grid-cols-4 gap-4">
                {[
                    {
                        label: "Total Applicants",
                        value: (metrics as any)?.totalApplications ?? 0,
                        icon: Users,
                        color: "text-blue-600",
                        bg: "bg-blue-50",
                    },
                    {
                        label: "Active Jobs",
                        value: activeJobs,
                        icon: ClipboardList,
                        color: "text-violet-600",
                        bg: "bg-violet-50",
                    },
                    {
                        label: "In Interview",
                        value: (metrics as any)?.interviewingCount ?? 0,
                        icon: Clock,
                        color: "text-amber-600",
                        bg: "bg-amber-50",
                    },
                    {
                        label: "Hired This Month",
                        value: (metrics as any)?.hiredCount ?? 0,
                        icon: CheckCircle2,
                        color: "text-emerald-600",
                        bg: "bg-emerald-50",
                    },
                ].map(({ label, value, icon: Icon, color, bg }) => (
                    <div key={label} className="bg-white rounded-2xl border border-slate-200 p-5 shadow-sm">
                        <div className={`w-10 h-10 ${bg} rounded-xl flex items-center justify-center mb-3`}>
                            <Icon className={`w-5 h-5 ${color}`} />
                        </div>
                        <p className="text-2xl font-extrabold text-slate-900">{value}</p>
                        <p className="text-xs text-slate-500 font-medium mt-0.5">{label}</p>
                    </div>
                ))}
            </div>

            {/* Main Nav Cards */}
            <div className="grid grid-cols-1 sm:grid-cols-2 gap-5">
                {/* Kanban / Pipeline Board */}
                <Link
                    href="/dashboard/applications?view=kanban"
                    className="group relative bg-white border border-slate-200 rounded-2xl p-6 shadow-sm hover:shadow-lg hover:border-primary/40 transition-all overflow-hidden"
                >
                    <div className="absolute inset-0 bg-gradient-to-br from-primary/5 to-transparent opacity-0 group-hover:opacity-100 transition-opacity" />
                    <div className="relative">
                        <div className="flex items-start justify-between mb-4">
                            <div className="w-12 h-12 bg-primary/10 rounded-2xl flex items-center justify-center">
                                <Kanban className="w-6 h-6 text-primary" />
                            </div>
                            <ArrowRight className="w-5 h-5 text-slate-300 group-hover:text-primary group-hover:translate-x-1 transition-all" />
                        </div>
                        <h2 className="text-lg font-bold text-slate-900 mb-1">Kanban Board</h2>
                        <p className="text-sm text-slate-500">
                            Drag and drop candidates through your hiring stages. Filter, bulk-move, and take stage-specific actions.
                        </p>
                        <div className="flex flex-wrap gap-2 mt-4">
                            {["Drag & Drop", "Filters", "Bulk Move", "Quick Actions", "Undo"].map(tag => (
                                <span key={tag} className="text-[11px] font-semibold px-2 py-0.5 bg-slate-100 text-slate-600 rounded-full">
                                    {tag}
                                </span>
                            ))}
                        </div>
                    </div>
                </Link>

                {/* Pipeline Manager */}
                <Link
                    href="/dashboard/ats/pipelines"
                    className="group relative bg-white border border-slate-200 rounded-2xl p-6 shadow-sm hover:shadow-lg hover:border-violet-400/50 transition-all overflow-hidden"
                >
                    <div className="absolute inset-0 bg-gradient-to-br from-violet-50 to-transparent opacity-0 group-hover:opacity-100 transition-opacity" />
                    <div className="relative">
                        <div className="flex items-start justify-between mb-4">
                            <div className="w-12 h-12 bg-violet-100 rounded-2xl flex items-center justify-center">
                                <GitBranch className="w-6 h-6 text-violet-600" />
                            </div>
                            <ArrowRight className="w-5 h-5 text-slate-300 group-hover:text-violet-500 group-hover:translate-x-1 transition-all" />
                        </div>
                        <h2 className="text-lg font-bold text-slate-900 mb-1">Pipeline Manager</h2>
                        <p className="text-sm text-slate-500">
                            Build custom hiring pipelines with drag-and-drop stages. Set entry/exit rules, time limits, and email automations per stage.
                        </p>
                        <div className="flex flex-wrap gap-2 mt-4">
                            {["Custom Stages", "Templates", "Stage Config", "Automations"].map(tag => (
                                <span key={tag} className="text-[11px] font-semibold px-2 py-0.5 bg-violet-50 text-violet-700 rounded-full">
                                    {tag}
                                </span>
                            ))}
                        </div>
                    </div>
                </Link>

                {/* Analytics (coming soon) */}
                <Link
                    href="/dashboard/applications"
                    className="group relative bg-white border border-slate-200 rounded-2xl p-6 shadow-sm hover:shadow-lg hover:border-emerald-400/50 transition-all overflow-hidden"
                >
                    <div className="absolute inset-0 bg-gradient-to-br from-emerald-50 to-transparent opacity-0 group-hover:opacity-100 transition-opacity" />
                    <div className="relative">
                        <div className="flex items-start justify-between mb-4">
                            <div className="w-12 h-12 bg-emerald-100 rounded-2xl flex items-center justify-center">
                                <BarChart3 className="w-6 h-6 text-emerald-600" />
                            </div>
                            <ArrowRight className="w-5 h-5 text-slate-300 group-hover:text-emerald-500 group-hover:translate-x-1 transition-all" />
                        </div>
                        <h2 className="text-lg font-bold text-slate-900 mb-1">Hiring Analytics</h2>
                        <p className="text-sm text-slate-500">
                            Visualize your hiring funnel, time-to-hire, and source breakdowns across all your open positions.
                        </p>
                        <div className="flex flex-wrap gap-2 mt-4">
                            {["Funnel View", "Source Metrics", "Interview Analytics"].map(tag => (
                                <span key={tag} className="text-[11px] font-semibold px-2 py-0.5 bg-emerald-50 text-emerald-700 rounded-full">
                                    {tag}
                                </span>
                            ))}
                        </div>
                    </div>
                </Link>

                {/* AI Scoring */}
                <div className="relative bg-gradient-to-br from-slate-900 to-slate-800 rounded-2xl p-6 shadow-sm overflow-hidden">
                    <div className="absolute top-0 right-0 w-32 h-32 bg-primary/20 rounded-full blur-3xl" />
                    <div className="relative">
                        <div className="flex items-start justify-between mb-4">
                            <div className="w-12 h-12 bg-white/10 rounded-2xl flex items-center justify-center">
                                <Sparkles className="w-6 h-6 text-yellow-400" />
                            </div>
                            <span className="text-[10px] font-bold px-2 py-1 bg-yellow-400/20 text-yellow-300 rounded-full border border-yellow-400/30">
                                AI Powered
                            </span>
                        </div>
                        <h2 className="text-lg font-bold text-white mb-1">AI Candidate Scoring</h2>
                        <p className="text-sm text-slate-400">
                            Let Gemini AI score and rank candidates against job requirements automatically — skills, experience, and culture fit.
                        </p>
                        <div className="flex flex-wrap gap-2 mt-4">
                            {["Skill Match", "Experience Fit", "Culture Score", "Summary"].map(tag => (
                                <span key={tag} className="text-[11px] font-semibold px-2 py-0.5 bg-white/10 text-slate-300 rounded-full">
                                    {tag}
                                </span>
                            ))}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    );
}
