"use client";

import Link from "next/link";
import { ArrowUpDown, FileText } from "lucide-react";

interface ApplicationsListProps {
    applications: any[];
    userRole?: string;
}

const statusConfig = {
    pending: {
        label: "Screening",
        className: "border-orange-400 text-orange-700 bg-orange-50"
    },
    reviewing: {
        label: "Shortlisted",
        className: "border-cyan-400 text-cyan-700 bg-cyan-50"
    },
    interview: {
        label: "Interviewing",
        className: "border-blue-400 text-blue-700 bg-blue-50"
    },
    offer: {
        label: "Job Offer",
        className: "border-emerald-400 text-emerald-700 bg-emerald-50"
    },
    hired: {
        label: "Hired",
        className: "border-emerald-400 text-emerald-700 bg-emerald-50"
    },
    rejected: {
        label: "Rejected",
        className: "border-red-400 text-red-700 bg-red-50"
    },
    withdrawn: {
        label: "Withdrawn",
        className: "border-slate-400 text-slate-700 bg-slate-50"
    },
};

export function ApplicationsList({ applications, userRole = "employer" }: ApplicationsListProps) {
    const isCandidate = userRole === "candidate";

    return (
        <div className="overflow-hidden rounded-2xl border border-slate-100 bg-white shadow-sm">
            {/* Table */}
            <div className="overflow-x-auto">
                <table className="w-full">
                    <thead>
                        <tr className="border-b border-slate-100 bg-slate-50/30">
                            <th className="px-6 py-4 text-left shadow-[inset_0_-1px_0_theme(colors.slate.100)]">
                                <div className="flex items-center gap-1.5 text-xs font-semibold uppercase tracking-wide text-slate-600">
                                    {isCandidate ? "Company" : "Name"}
                                    <ArrowUpDown className="h-3 w-3" />
                                </div>
                            </th>
                            {!isCandidate && (
                                <th className="px-6 py-4 text-left shadow-[inset_0_-1px_0_theme(colors.slate.100)]">
                                    <div className="flex items-center gap-1.5 text-xs font-semibold uppercase tracking-wide text-slate-600">
                                        Email
                                        <ArrowUpDown className="h-3 w-3" />
                                    </div>
                                </th>
                            )}
                            <th className="px-6 py-4 text-left shadow-[inset_0_-1px_0_theme(colors.slate.100)]">
                                <div className="flex items-center gap-1.5 text-xs font-semibold uppercase tracking-wide text-slate-600">
                                    {isCandidate ? "Job Title" : "Role"}
                                    <ArrowUpDown className="h-3 w-3" />
                                </div>
                            </th>
                            <th className="px-6 py-4 text-left shadow-[inset_0_-1px_0_theme(colors.slate.100)]">
                                <div className="flex items-center gap-1.5 text-xs font-semibold uppercase tracking-wide text-slate-600">
                                    Date
                                    <ArrowUpDown className="h-3 w-3" />
                                </div>
                            </th>
                            {!isCandidate && (
                                <>
                                    <th className="px-6 py-4 text-left shadow-[inset_0_-1px_0_theme(colors.slate.100)]">
                                        <div className="flex items-center gap-1.5 text-xs font-semibold uppercase tracking-wide text-slate-600">
                                            Skills
                                            <ArrowUpDown className="h-3 w-3" />
                                        </div>
                                    </th>
                                    <th className="px-6 py-4 text-left shadow-[inset_0_-1px_0_theme(colors.slate.100)]">
                                        <div className="flex items-center gap-1.5 text-xs font-semibold uppercase tracking-wide text-slate-600">
                                            Resume
                                            <ArrowUpDown className="h-3 w-3" />
                                        </div>
                                    </th>
                                </>
                            )}
                            <th className="px-6 py-4 text-left shadow-[inset_0_-1px_0_theme(colors.slate.100)]">
                                <div className="flex items-center gap-1.5 text-xs font-semibold uppercase tracking-wide text-slate-600">
                                    Status
                                    <ArrowUpDown className="h-3 w-3" />
                                </div>
                            </th>
                        </tr>
                    </thead>
                    <tbody className="divide-y divide-slate-50">
                        {applications.map(application => {
                            const candidate = application.candidate;
                            const job = application.job;

                            if (!job || (!candidate && !isCandidate)) return null;

                            // Safely extract skills, falling back to empty array
                            const skills = candidate?.profile?.skills || candidate?.skills || [];

                            return (
                                <tr key={application.id} className="group hover:bg-slate-50/50 transition-colors">
                                    {/* Name / Company */}
                                    <td className="px-6 py-4">
                                        {isCandidate ? (
                                            <Link href={`/dashboard/applications/${application.id}`} className="flex items-center gap-3">
                                                <div className="flex h-10 w-10 items-center justify-center rounded-lg bg-slate-100 text-sm font-bold text-slate-500 shrink-0">
                                                    {(job.company || 'C').charAt(0)}
                                                </div>
                                                <span className="font-semibold text-slate-900 group-hover:text-primary transition-colors">
                                                    {job.company}
                                                </span>
                                            </Link>
                                        ) : (
                                            <Link
                                                href={`/dashboard/applications/${application.id}`}
                                                className="flex items-center gap-3"
                                            >
                                                <div className="flex h-10 w-10 items-center justify-center rounded-full bg-gradient-to-br from-primary to-accent text-sm font-bold text-white shrink-0">
                                                    {(candidate.name || 'U').charAt(0).toUpperCase()}
                                                </div>
                                                <span className="font-semibold text-slate-900 group-hover:text-primary transition-colors">
                                                    {candidate.name}
                                                </span>
                                            </Link>
                                        )}
                                    </td>

                                    {/* Email (Employer only) */}
                                    {!isCandidate && (
                                        <td className="px-6 py-4">
                                            <span className="text-sm text-slate-600">
                                                {candidate.email}
                                            </span>
                                        </td>
                                    )}

                                    {/* Role / Job Title */}
                                    <td className="px-6 py-4">
                                        <span className="text-sm font-medium text-slate-700">
                                            {job.title}
                                        </span>
                                    </td>

                                    {/* Date */}
                                    <td className="px-6 py-4">
                                        <span className="text-sm text-slate-600">
                                            {new Date(application.applied_at || application.appliedAt).toLocaleDateString('en-US', {
                                                month: 'short',
                                                day: 'numeric',
                                                year: 'numeric'
                                            })}
                                        </span>
                                    </td>

                                    {/* Skills & Resume (Employer only) */}
                                    {!isCandidate && (
                                        <>
                                            <td className="px-6 py-4">
                                                {skills.length > 0 ? (
                                                    <div className="flex flex-wrap gap-1">
                                                        {skills.slice(0, 2).map((skill: any, index: number) => (
                                                            <span key={index} className="px-2 py-0.5 text-xs font-medium rounded bg-slate-100 text-slate-600">
                                                                {skill.name || skill}
                                                            </span>
                                                        ))}
                                                        {skills.length > 2 && (
                                                            <span className="px-2 py-0.5 text-xs font-medium rounded bg-slate-100 text-slate-600">
                                                                +{skills.length - 2}
                                                            </span>
                                                        )}
                                                    </div>
                                                ) : (
                                                    <span className="text-sm text-slate-400">-</span>
                                                )}
                                            </td>
                                            <td className="px-6 py-4">
                                                {application.resume_url || application.resumeUrl ? (
                                                    <a
                                                        href={application.resume_url || application.resumeUrl}
                                                        target="_blank"
                                                        rel="noopener noreferrer"
                                                        onClick={(e) => e.stopPropagation()}
                                                        className="inline-flex items-center gap-1.5 text-sm font-medium text-primary hover:underline"
                                                    >
                                                        <FileText className="h-4 w-4" />
                                                        View Resume
                                                    </a>
                                                ) : (
                                                    <span className="text-sm text-slate-400">-</span>
                                                )}
                                            </td>
                                        </>
                                    )}

                                    {/* Status */}
                                    <td className="px-6 py-4">
                                        <span className={`inline-block border px-3 py-1.5 rounded-full text-xs font-bold tracking-wide ${statusConfig[application.status as keyof typeof statusConfig]?.className || statusConfig.pending.className}`}>
                                            {statusConfig[application.status as keyof typeof statusConfig]?.label || application.status.toUpperCase()}
                                        </span>
                                    </td>
                                </tr>
                            );
                        })}
                    </tbody>
                </table>
            </div>

            {/* Footer with pagination info */}
            {applications.length > 0 && (
                <div className="border-t border-slate-100 px-6 py-4">
                    <p className="text-sm text-slate-500">
                        Showing 1 to {applications.length} of {applications.length} entries
                    </p>
                </div>
            )}
        </div>
    );
}
