"use client";

import { CheckCircle2 } from "lucide-react";
import { cn } from "@/lib/utils";
import { Card, CardContent } from "@/components/ui/card";
import { Badge } from "@/components/ui/badge";
import { Progress } from "@/components/ui/progress";

interface ApplicationStep {
    name: string;
    completed: boolean;
}

interface ApplicationData {
    id: string;
    company: string;
    role: string;
    status: string;
    date: string;
}

function getStepsFromStatus(status: string): ApplicationStep[] {
    const statusOrder = ["pending", "reviewing", "interview", "offered"];
    const currentIdx = statusOrder.indexOf(status);

    return [
        { name: "Applied", completed: true },
        { name: "Review", completed: currentIdx >= 1 },
        { name: "Interview", completed: currentIdx >= 2 },
        { name: "Result", completed: currentIdx >= 3 },
    ];
}

function getStatusBadge(status: string) {
    const config: Record<string, { label: string; variant: "default" | "secondary" | "destructive" | "outline" }> = {
        pending: { label: "Screening", variant: "secondary" },
        reviewing: { label: "In Review", variant: "default" },
        interview: { label: "Interviewing", variant: "default" },
        offered: { label: "Offered", variant: "default" },
        rejected: { label: "Rejected", variant: "destructive" },
    };
    const c = config[status] || config.pending;
    return <Badge variant={c.variant}>{c.label}</Badge>;
}

type JobProgressProps = {
    applications?: ApplicationData[];
};

export function JobProgress({ applications }: JobProgressProps) {
    const data = applications || [];

    if (data.length === 0) {
        return (
            <div className="flex flex-col items-center justify-center py-8 text-center">
                <p className="text-sm text-slate-500">No recent applications to display.</p>
            </div>
        );
    }

    return (
        <div className="space-y-4">
            {data.map((app) => {
                const steps = getStepsFromStatus(app.status);
                const progressValue = (steps.filter(s => s.completed).length / steps.length) * 100;

                return (
                    <Card key={app.id} className="shadow-sm">
                        <CardContent className="p-6">
                            <div className="flex items-start justify-between">
                                <div className="flex gap-4">
                                    <div className="flex h-12 w-12 items-center justify-center rounded-xl bg-slate-100 font-bold text-slate-400 border border-slate-200">
                                        {app.company.charAt(0)}
                                    </div>
                                    <div>
                                        <h4 className="font-bold text-slate-900">{app.role}</h4>
                                        <p className="text-sm text-slate-500">
                                            {app.company} &bull; {new Date(app.date).toLocaleDateString("en-US", { month: "short", day: "numeric", year: "numeric" })}
                                        </p>
                                    </div>
                                </div>
                                {getStatusBadge(app.status)}
                            </div>

                            <div className="mt-8">
                                <div className="flex items-center justify-between px-2">
                                    {steps.map((step, idx) => (
                                        <div key={idx} className="flex flex-col items-center gap-2">
                                            {step.completed ? (
                                                <CheckCircle2 className="h-5 w-5 text-emerald-500" />
                                            ) : (
                                                <div className="h-5 w-5 rounded-full border-2 border-slate-200 bg-white" />
                                            )}
                                            <span className={cn("text-[10px] font-bold uppercase tracking-wider", step.completed ? "text-slate-900" : "text-slate-400")}>
                                                {step.name}
                                            </span>
                                        </div>
                                    ))}
                                </div>
                                <Progress value={progressValue} className="mt-2 h-1.5" />
                            </div>
                        </CardContent>
                    </Card>
                );
            })}
        </div>
    );
}
