import { format } from "date-fns";
import { DollarSign, Briefcase, MapPin, Search } from "lucide-react";
import dynamic from 'next/dynamic';

const JobDescriptionRenderer = dynamic(
    () => import('@/components/jobs/JobDescriptionRenderer').then(m => m.JobDescriptionRenderer),
    { ssr: false }
);

export function JobDetailsTab({ job }: { job: any }) {

    const renderArray = (data: any, title: string) => {
        if (!data) return null;
        let parsed: string[] = [];
        if (typeof data === 'string') {
            try { parsed = JSON.parse(data); } catch { return null; }
        } else if (Array.isArray(data)) {
            parsed = data;
        }

        if (parsed.length === 0) return null;

        return (
            <div className="mb-6">
                <h4 className="font-semibold text-slate-900 mb-2">{title}</h4>
                <ul className="list-disc list-inside space-y-1 text-slate-700">
                    {parsed.map((item, idx) => (
                        <li key={idx} className="pl-2">{item}</li>
                    ))}
                </ul>
            </div>
        );
    };

    return (
        <div className="space-y-8 animate-in fade-in duration-300">

            {/* Quick Overview */}
            <div>
                <h3 className="text-xl font-bold text-slate-900 mb-4">Job Offer Overview</h3>
                <div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-4 gap-4">
                    <div className="bg-slate-50 p-4 rounded-xl border border-slate-200">
                        <DollarSign className="w-5 h-5 text-slate-400 mb-2" />
                        <p className="text-xs text-slate-500 font-medium">Compensation</p>
                        <p className="font-semibold text-slate-800">
                            {job.hide_salary ? 'Hidden' : (job.salary_min ? `${job.currency} ${job.salary_min.toLocaleString()} / ${job.salary_type || 'yr'}` : 'Not Spec.')}
                        </p>
                    </div>
                    <div className="bg-slate-50 p-4 rounded-xl border border-slate-200">
                        <Briefcase className="w-5 h-5 text-slate-400 mb-2" />
                        <p className="text-xs text-slate-500 font-medium">Job Type</p>
                        <p className="font-semibold text-slate-800 capitalize">{job.type.replace('-', ' ')}</p>
                    </div>
                    <div className="bg-slate-50 p-4 rounded-xl border border-slate-200">
                        <MapPin className="w-5 h-5 text-slate-400 mb-2" />
                        <p className="text-xs text-slate-500 font-medium">Location</p>
                        <p className="font-semibold text-slate-800">
                            {job.work_location_type === 'remote' ? 'Remote' : (job.city || 'Office')}
                        </p>
                    </div>
                    <div className="bg-slate-50 p-4 rounded-xl border border-slate-200">
                        <Search className="w-5 h-5 text-slate-400 mb-2" />
                        <p className="text-xs text-slate-500 font-medium">Application Method</p>
                        <p className="font-semibold text-slate-800 capitalize">{job.application_method}</p>
                    </div>
                </div>
            </div>

            <hr className="border-slate-100" />

            {/* Description */}
            <div>
                <h3 className="text-lg font-bold text-slate-900 mb-4">Job Description</h3>
                {job.summary && (
                    <div className="p-4 bg-blue-50 border border-blue-100 rounded-xl mb-4 text-blue-900 text-sm">
                        <span className="font-semibold">Summary:</span> {job.summary}
                    </div>
                )}

                <JobDescriptionRenderer
                    html={job.description || '<p class="text-slate-400 italic">No description provided...</p>'}
                    className="prose prose-sm prose-slate max-w-none prose-a:text-blue-600 hover:prose-a:underline"
                />
            </div>

            <hr className="border-slate-100" />

            {/* Requirements & Benefits Arrays */}
            <div>
                <h3 className="text-lg font-bold text-slate-900 mb-4">Requirements & Offerings</h3>
                <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        {renderArray(job.responsibilities, "Key Responsibilities")}
                        {renderArray(job.required_qualifications, "Required Qualifications")}
                    </div>
                    <div>
                        {renderArray(job.preferred_qualifications, "Preferred Qualifications")}
                        {renderArray(job.benefits, "Benefits & Perks")}
                    </div>
                </div>
            </div>

            {/* Application Details */}
            {job.application_method !== 'internal' && (
                <>
                    <hr className="border-slate-100" />
                    <div>
                        <h3 className="text-lg font-bold text-slate-900 mb-4">External Routing</h3>
                        <div className="bg-amber-50 p-4 rounded-xl border border-amber-200">
                            <p className="text-sm text-amber-800 mb-2">This job drives candidates away from the platform. Verify the destination link.</p>
                            {job.application_method === 'external' && (
                                <p className="font-medium text-amber-900 break-all overflow-hidden flex flex-wrap"><span className="text-slate-500 mr-2">URL:</span> <a href={job.external_url} target="_blank" rel="noopener noreferrer" className="underline">{job.external_url}</a></p>
                            )}
                            {job.application_method === 'email' && (
                                <p className="font-medium text-amber-900"><span className="text-slate-500 mr-2">Email:</span> {job.contact_email}</p>
                            )}
                        </div>
                    </div>
                </>
            )}

        </div>
    );
}
