"use client";

import Link from "next/link";
import { ArrowUpDown, MapPin, Users, Eye } from "lucide-react";

interface JobsTableProps {
    jobs: any[];
    baseUrl?: string;
}

const typeColors: Record<string, string> = {
    "Full-time": "border-emerald-400 text-emerald-700 bg-emerald-50",
    "Part-time": "border-orange-400 text-orange-700 bg-orange-50",
    "Contract": "border-cyan-400 text-cyan-700 bg-cyan-50",
    "Freelance": "border-purple-400 text-purple-700 bg-purple-50",
    "Remote": "border-blue-400 text-blue-700 bg-blue-50",
};

export function JobsTable({ jobs, baseUrl = "/jobs" }: JobsTableProps) {
    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">
                                <div className="flex items-center gap-1.5 text-xs font-semibold uppercase tracking-wide text-slate-600">
                                    Job Title
                                    <ArrowUpDown className="h-3 w-3" />
                                </div>
                            </th>
                            <th className="px-6 py-4 text-left">
                                <div className="flex items-center gap-1.5 text-xs font-semibold uppercase tracking-wide text-slate-600">
                                    Company
                                    <ArrowUpDown className="h-3 w-3" />
                                </div>
                            </th>
                            <th className="px-6 py-4 text-left">
                                <div className="flex items-center gap-1.5 text-xs font-semibold uppercase tracking-wide text-slate-600">
                                    Location
                                    <ArrowUpDown className="h-3 w-3" />
                                </div>
                            </th>
                            <th className="px-6 py-4 text-left">
                                <div className="flex items-center gap-1.5 text-xs font-semibold uppercase tracking-wide text-slate-600">
                                    Salary Range
                                    <ArrowUpDown className="h-3 w-3" />
                                </div>
                            </th>
                            <th className="px-6 py-4 text-left">
                                <div className="flex items-center gap-1.5 text-xs font-semibold uppercase tracking-wide text-slate-600">
                                    Applicants
                                    <ArrowUpDown className="h-3 w-3" />
                                </div>
                            </th>
                            <th className="px-6 py-4 text-left">
                                <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>
                            <th className="px-6 py-4 text-left">
                                <div className="flex items-center gap-1.5 text-xs font-semibold uppercase tracking-wide text-slate-600">
                                    Posted
                                    <ArrowUpDown className="h-3 w-3" />
                                </div>
                            </th>
                            <th className="px-6 py-4 text-left">
                                <div className="flex items-center gap-1.5 text-xs font-semibold uppercase tracking-wide text-slate-600">
                                    Actions
                                    <ArrowUpDown className="h-3 w-3" />
                                </div>
                            </th>
                        </tr>
                    </thead>
                    <tbody className="divide-y divide-slate-50">
                        {jobs.length === 0 ? (
                            <tr>
                                <td colSpan={8} className="px-6 py-12 text-center text-sm text-slate-400">
                                    No jobs posted yet
                                </td>
                            </tr>
                        ) : (
                            jobs.map(job => (
                                <tr key={job.id} className="group hover:bg-slate-50/50 transition-colors">
                                    {/* Job Title with Company Logo */}
                                    <td className="px-6 py-4">
                                        <Link
                                            href={`${baseUrl}/${job.id}`}
                                            className="flex items-center gap-3"
                                        >
                                            <div className="flex h-10 w-10 items-center justify-center rounded-lg bg-gradient-to-br from-primary to-accent text-sm font-bold text-white shrink-0">
                                                {job.company.charAt(0).toUpperCase()}
                                            </div>
                                            <div className="min-w-0">
                                                <span className="block font-semibold text-slate-900 group-hover:text-primary transition-colors truncate">
                                                    {job.title}
                                                </span>
                                                {job.featured && (
                                                    <span className="inline-block text-xs font-medium text-primary">Featured</span>
                                                )}
                                            </div>
                                        </Link>
                                    </td>

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

                                    {/* Location */}
                                    <td className="px-6 py-4">
                                        <span className="inline-flex items-center gap-1.5 text-sm text-slate-600">
                                            <MapPin className="h-4 w-4" />
                                            {job.location}
                                        </span>
                                    </td>

                                    {/* Salary Range */}
                                    <td className="px-6 py-4">
                                        {job.salaryMin && job.salaryMax ? (
                                            <span className="text-sm font-medium text-slate-700">
                                                ${job.salaryMin.toLocaleString()} - ${job.salaryMax.toLocaleString()}
                                            </span>
                                        ) : (
                                            <span className="text-sm text-slate-400">-</span>
                                        )}
                                    </td>

                                    {/* Applicants */}
                                    <td className="px-6 py-4">
                                        <span className="inline-flex items-center gap-1.5 text-sm font-semibold text-primary">
                                            <Users className="h-4 w-4" />
                                            {job.applicants || 0}
                                        </span>
                                    </td>

                                    {/* Status Badge */}
                                    <td className="px-6 py-4">
                                        <span className={`inline-block rounded-lg px-3 py-1.5 text-xs font-bold tracking-wide ${job.status === 'published' ? 'bg-green-100 text-green-700' :
                                            job.status === 'pending' ? 'bg-amber-100 text-amber-700' :
                                                job.status === 'changes_requested' ? 'bg-red-100 text-red-700' :
                                                    job.status === 'rejected' ? 'bg-red-200 text-red-800' :
                                                        'bg-slate-100 text-slate-700'
                                            }`}>
                                            {job.status ? job.status.replace('_', ' ').toUpperCase() : 'UNKNOWN'}
                                        </span>
                                    </td>

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

                                    {/* Actions */}
                                    <td className="px-6 py-4">
                                        <div className="flex items-center gap-3">
                                            <Link
                                                href={`${baseUrl}/${job.id}`}
                                                className="inline-flex items-center gap-1 text-sm font-medium text-slate-500 hover:text-primary transition-colors"
                                                title="View Public Details"
                                            >
                                                <Eye className="h-4 w-4" />
                                            </Link>
                                            {(job.status === 'changes_requested' || job.status === 'draft') && (
                                                <Link
                                                    href={`/dashboard/jobs/${job.id}/edit`}
                                                    className="inline-flex items-center text-xs font-bold text-red-600 hover:text-red-700 underline"
                                                >
                                                    Edit
                                                </Link>
                                            )}
                                        </div>
                                    </td>
                                </tr>
                            ))
                        )}
                    </tbody>
                </table>
            </div>

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