"use client";

import { BarChart3, ArrowRight, Globe, UserPlus, Search, Share2 } from "lucide-react";

interface FunnelStage {
    id: string;
    label: string;
    color: string;
    count: number;
    percentage: number;
    conversionRate: number;
}

interface Source {
    name: string;
    count: number;
    percentage: number;
}

interface HiringFunnelProps {
    stages: FunnelStage[];
    sources: Source[];
    totalApplications: number;
}

const sourceIcons: Record<string, any> = {
    Direct: Globe,
    direct: Globe,
    referral: UserPlus,
    platform_search: Search,
    job_board: Share2,
};

export function HiringFunnel({ stages, sources, totalApplications }: HiringFunnelProps) {
    if (totalApplications === 0) {
        return (
            <div className="bg-white rounded-2xl border border-slate-100 p-8 shadow-sm">
                <div className="text-center py-8">
                    <BarChart3 className="w-10 h-10 mx-auto mb-3 text-slate-300" />
                    <h3 className="text-base font-bold text-slate-600">No Data Yet</h3>
                    <p className="text-sm text-slate-400 mt-1">
                        Funnel analytics will appear once you receive applications.
                    </p>
                </div>
            </div>
        );
    }

    // Pipeline stages only (exclude rejected for funnel viz)
    const pipelineStages = stages.filter((s) => s.id !== "rejected");
    const rejectedStage = stages.find((s) => s.id === "rejected");
    const maxCount = Math.max(...pipelineStages.map((s) => s.count), 1);

    return (
        <div className="bg-white rounded-2xl border border-slate-100 shadow-sm overflow-hidden">
            {/* Header */}
            <div className="px-6 py-4 border-b border-slate-100 flex items-center justify-between">
                <div className="flex items-center gap-3">
                    <div className="w-9 h-9 rounded-xl bg-violet-50 flex items-center justify-center">
                        <BarChart3 className="w-5 h-5 text-violet-600" />
                    </div>
                    <div>
                        <h3 className="text-base font-bold text-slate-900">
                            Hiring Funnel
                        </h3>
                        <p className="text-xs text-slate-500">
                            {totalApplications} total applications
                        </p>
                    </div>
                </div>
            </div>

            <div className="p-6 space-y-6">
                {/* Funnel Bars */}
                <div className="space-y-3">
                    {pipelineStages.map((stage, i) => {
                        const widthPercent = Math.max(
                            (stage.count / maxCount) * 100,
                            stage.count > 0 ? 8 : 2
                        );

                        return (
                            <div key={stage.id}>
                                <div className="flex items-center justify-between mb-1.5">
                                    <span className="text-sm font-semibold text-slate-700">
                                        {stage.label}
                                    </span>
                                    <div className="flex items-center gap-2">
                                        <span className="text-sm font-bold text-slate-900">
                                            {stage.count}
                                        </span>
                                        <span className="text-xs text-slate-400">
                                            ({stage.percentage}%)
                                        </span>
                                    </div>
                                </div>
                                <div className="relative h-8 bg-slate-50 rounded-lg overflow-hidden">
                                    <div
                                        className={`absolute inset-y-0 left-0 ${stage.color} rounded-lg transition-all duration-700 ease-out flex items-center justify-end pr-2`}
                                        style={{ width: `${widthPercent}%` }}
                                    >
                                        {widthPercent > 15 && (
                                            <span className="text-[10px] font-bold text-white/80">
                                                {stage.percentage}%
                                            </span>
                                        )}
                                    </div>
                                </div>

                                {/* Conversion arrow between stages */}
                                {i < pipelineStages.length - 1 && (
                                    <div className="flex items-center gap-1.5 ml-4 mt-1 text-[10px] text-slate-400">
                                        <ArrowRight className="w-3 h-3" />
                                        <span>
                                            {pipelineStages[i + 1].conversionRate}% conversion
                                        </span>
                                    </div>
                                )}
                            </div>
                        );
                    })}

                    {/* Rejected (separate, muted) */}
                    {rejectedStage && rejectedStage.count > 0 && (
                        <div className="pt-2 border-t border-slate-100">
                            <div className="flex items-center justify-between mb-1.5">
                                <span className="text-sm font-semibold text-red-400">
                                    {rejectedStage.label}
                                </span>
                                <div className="flex items-center gap-2">
                                    <span className="text-sm font-bold text-red-500">
                                        {rejectedStage.count}
                                    </span>
                                    <span className="text-xs text-slate-400">
                                        ({rejectedStage.percentage}%)
                                    </span>
                                </div>
                            </div>
                            <div className="relative h-6 bg-red-50/50 rounded-lg overflow-hidden">
                                <div
                                    className="absolute inset-y-0 left-0 bg-red-300 rounded-lg"
                                    style={{
                                        width: `${Math.max(
                                            (rejectedStage.count / maxCount) * 100,
                                            rejectedStage.count > 0 ? 5 : 0
                                        )}%`,
                                    }}
                                />
                            </div>
                        </div>
                    )}
                </div>

                {/* Source Breakdown */}
                {sources.length > 0 && (
                    <div className="pt-4 border-t border-slate-100">
                        <h4 className="text-xs font-bold text-slate-500 uppercase tracking-wider mb-3">
                            Application Sources
                        </h4>
                        <div className="grid grid-cols-2 sm:grid-cols-4 gap-3">
                            {sources.map((source) => {
                                const Icon =
                                    sourceIcons[source.name] || Globe;
                                return (
                                    <div
                                        key={source.name}
                                        className="p-3 bg-slate-50 rounded-xl border border-slate-100 text-center"
                                    >
                                        <Icon className="w-4 h-4 mx-auto text-slate-400 mb-1.5" />
                                        <p className="text-lg font-extrabold text-slate-900">
                                            {source.count}
                                        </p>
                                        <p className="text-[10px] font-semibold text-slate-500 capitalize">
                                            {source.name.replace(/_/g, " ")}
                                        </p>
                                    </div>
                                );
                            })}
                        </div>
                    </div>
                )}
            </div>
        </div>
    );
}
