import Image from 'next/image';
import { formatDistanceToNow } from 'date-fns';
import {
    MapPin, Briefcase, DollarSign, Calendar, Clock,
    AlertTriangle, Info, CheckCircle, ExternalLink, ShieldCheck
} from 'lucide-react';

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

    // Helper for rendering skills / list of items
    const renderList = (data: any) => {
        if (!data) return [];
        try {
            const parsed = typeof data === 'string' ? JSON.parse(data) : data;
            return Array.isArray(parsed) ? parsed : [];
        } catch (e) {
            return [];
        }
    };

    const requirements = renderList(job.required_qualifications);
    const responsibilities = renderList(job.responsibilities || '["Typical tasks aligned with this role", "Collaborate with team"]'); // Mock if missing since db might not have it isolated
    const benefits = renderList(job.benefits);

    return (
        <div className="p-8 space-y-10">
            {/* SECTION 1: Header Information */}
            <section className="space-y-6">
                <div>
                    <h1 className="text-3xl font-extrabold text-slate-900 tracking-tight">{job.title}</h1>
                    <div className="flex items-center gap-4 mt-3">
                        <span className="text-sm font-medium text-slate-500 bg-slate-100 px-2.5 py-1 rounded-md">
                            {job.title.length} characters
                        </span>
                        <button className="text-sm font-medium text-blue-600 hover:text-blue-700">Edit Title</button>
                    </div>
                </div>

                <div className="bg-white rounded-xl border border-slate-200 p-5 flex items-start gap-5 shadow-sm">
                    <div className="w-20 h-20 rounded-xl bg-slate-50 border border-slate-100 flex items-center justify-center overflow-hidden relative shrink-0">
                        {job.employer?.avatar_url ? (
                            <Image src={job.employer.avatar_url} alt={job.company} fill className="object-cover" />
                        ) : (
                            <span className="text-3xl font-bold text-slate-400">{job.company?.charAt(0) || 'C'}</span>
                        )}
                    </div>
                    <div>
                        <div className="flex items-center gap-2">
                            <h2 className="text-xl font-bold text-slate-900">{job.company}</h2>
                            {/* Mock Verification Badge */}
                            <span className="flex items-center gap-1 text-xs font-medium text-green-700 bg-green-50 px-2 py-0.5 rounded-full border border-green-200">
                                <ShieldCheck className="w-3 h-3" /> Verified Company
                            </span>
                        </div>
                        <div className="flex items-center gap-4 mt-2 text-sm text-slate-600">
                            <span>15 jobs posted</span>
                            <span>•</span>
                            <span>93% approval rate</span>
                            <span>•</span>
                            <span>Member since Jan 2023</span>
                        </div>
                    </div>
                </div>

                <div className="grid grid-cols-2 gap-4">
                    <div className="flex justify-start gap-3 p-3 bg-white rounded-lg border border-slate-200">
                        <MapPin className="w-5 h-5 text-slate-400 mt-0.5" />
                        <div>
                            <p className="text-xs font-semibold text-slate-500 uppercase tracking-wider mb-0.5">Location</p>
                            <p className="text-sm font-medium text-slate-900">{job.work_location_type === 'remote' ? 'Remote' : (job.city || job.address || 'Unspecified')}</p>
                        </div>
                    </div>
                    <div className="flex justify-start gap-3 p-3 bg-white rounded-lg border border-slate-200">
                        <Briefcase className="w-5 h-5 text-slate-400 mt-0.5" />
                        <div>
                            <p className="text-xs font-semibold text-slate-500 uppercase tracking-wider mb-0.5">Job Type</p>
                            <p className="text-sm font-medium text-slate-900 capitalize">{job.type.replace('-', ' ')}</p>
                        </div>
                    </div>
                    <div className="flex justify-start gap-3 p-3 bg-white rounded-lg border border-slate-200">
                        <DollarSign className="w-5 h-5 text-slate-400 mt-0.5" />
                        <div>
                            <p className="text-xs font-semibold text-slate-500 uppercase tracking-wider mb-0.5">Salary</p>
                            <p className="text-sm font-medium text-slate-900">
                                {job.hide_salary ? 'Hidden' : (job.salary_min ? `${job.currency} ${job.salary_min.toLocaleString()} ${job.salary_max ? `- ${job.salary_max.toLocaleString()}` : ''}` : 'Not Specified')}
                            </p>
                        </div>
                    </div>
                    <div className="flex justify-start gap-3 p-3 bg-white rounded-lg border border-slate-200">
                        <Calendar className="w-5 h-5 text-slate-400 mt-0.5" />
                        <div>
                            <p className="text-xs font-semibold text-slate-500 uppercase tracking-wider mb-0.5">Posted / Expires</p>
                            <p className="text-sm font-medium text-slate-900">
                                {formatDistanceToNow(new Date(job.posted_at))} ago • 30 days
                            </p>
                        </div>
                    </div>
                </div>
            </section>

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

            {/* SECTION 2: Alert Banners */}
            <section className="space-y-3">
                <h3 className="text-xs font-bold text-slate-400 uppercase tracking-wider mb-2">System Alerts</h3>

                {(!job.salary_min || job.hide_salary) && (
                    <div className="bg-orange-50 border border-orange-200 rounded-lg p-4 flex gap-3">
                        <AlertTriangle className="w-5 h-5 text-orange-600 shrink-0 mt-0.5" />
                        <div>
                            <h4 className="text-sm font-bold text-orange-800">WARNINGS</h4>
                            <ul className="mt-1 text-sm text-orange-700 space-y-1 list-disc list-inside">
                                <li>No salary information provided</li>
                                {(!job.description || job.description.length < 200) && <li>Job description is quite short</li>}
                            </ul>
                            <p className="text-sm font-medium text-orange-800 mt-2">→ Consider: Request improvements before approval</p>
                        </div>
                    </div>
                )}

                <div className="bg-blue-50 border border-blue-200 rounded-lg p-4 flex gap-3">
                    <Info className="w-5 h-5 text-blue-600 shrink-0 mt-0.5" />
                    <div>
                        <h4 className="text-sm font-bold text-blue-800">INFORMATION</h4>
                        <ul className="mt-1 text-sm text-blue-700 space-y-1 list-disc list-inside">
                            <li>Similar jobs in this category average $75k-$95k</li>
                            <li>Company is highly rated by previous candidates</li>
                        </ul>
                    </div>
                </div>
            </section>

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

            {/* SECTION 3: Job Description */}
            <section className="space-y-4">
                <div className="flex items-center justify-between">
                    <h3 className="text-lg font-bold text-slate-900">About This Role</h3>
                    <div className="flex items-center gap-3">
                        <span className="text-xs font-medium text-slate-500 bg-slate-100 px-2 py-1 rounded-md border border-slate-200">
                            {job.description?.split(' ').length || 0} words
                        </span>
                        <span className="text-xs font-medium text-green-700 bg-green-50 px-2 py-1 rounded-md border border-green-200">
                            ✓ Professional Tone
                        </span>
                    </div>
                </div>

                <div className="prose prose-sm max-w-none text-slate-700 bg-white p-6 rounded-xl border border-slate-200">
                    {/* Simulated highlighting, normally wrapped by a parser based on AI analysis. 
                        We simply render description here with a styled block */}
                    <div className="whitespace-pre-wrap leading-relaxed">
                        {job.description || job.summary || "No detailed description provided."}
                    </div>
                </div>

                <div className="flex gap-2 text-xs font-medium">
                    <span className="px-2 py-1 bg-slate-100 text-slate-600 rounded">Tags:</span>
                    <span className="px-2 py-1 bg-blue-50 text-blue-700 rounded border border-blue-100">[Job Purpose]</span>
                    <span className="px-2 py-1 bg-blue-50 text-blue-700 rounded border border-blue-100">[Requirements]</span>
                    <span className="px-2 py-1 bg-slate-50 text-slate-500 rounded border border-slate-200 border-dashed">Missing [Culture]</span>
                </div>
            </section>

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

            {/* SECTION 4: Detailed Requirements */}
            <section className="space-y-8">
                <div>
                    <h3 className="text-lg font-bold text-slate-900 mb-4">Required Qualifications</h3>
                    <div className="bg-white border text-sm text-slate-700 border-slate-200 rounded-xl overflow-hidden divide-y divide-slate-100">
                        {requirements.length > 0 ? requirements.map((req: string, i: number) => (
                            <div key={i} className="p-4 flex items-start gap-3 hover:bg-slate-50 transition-colors">
                                <CheckCircle className="w-5 h-5 text-green-500 shrink-0" />
                                <div>
                                    <p className="font-medium">{req}</p>
                                    <p className="text-xs text-slate-500 mt-0.5">→ Standard requirement</p>
                                </div>
                            </div>
                        )) : (
                            <div className="p-4 text-slate-500 italic">No specific qualifications listed.</div>
                        )}
                        {/* Mock flag for demonstration */}
                        {requirements.length > 0 && (
                            <div className="p-4 flex items-start gap-3 bg-orange-50/50 hover:bg-orange-50 transition-colors">
                                <AlertTriangle className="w-5 h-5 text-orange-500 shrink-0" />
                                <div>
                                    <p className="font-medium text-slate-900">Must be available 24/7</p>
                                    <p className="text-xs text-orange-700 mt-0.5">→ Unrealistic - flag this</p>
                                </div>
                            </div>
                        )}
                    </div>
                </div>

                <div>
                    <h3 className="text-lg font-bold text-slate-900 mb-4">Daily Responsibilities</h3>
                    <ul className="space-y-3 bg-white p-6 rounded-xl border border-slate-200">
                        {responsibilities.map((resp: string, i: number) => (
                            <li key={i} className="flex gap-3 text-sm text-slate-700">
                                <span className="text-slate-400 mt-0.5">•</span>
                                {resp}
                            </li>
                        ))}
                    </ul>
                </div>
            </section>

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

            {/* SECTION 5: Compensation & Benefits */}
            <section className="space-y-6">
                <h3 className="text-lg font-bold text-slate-900">Compensation Package</h3>
                <div className="grid grid-cols-2 gap-4">
                    <div className="bg-white p-5 rounded-xl border border-slate-200">
                        <p className="text-xs font-bold text-slate-400 uppercase tracking-wider mb-2">Base Salary</p>
                        <p className="text-2xl font-bold text-slate-900">
                            {job.hide_salary ? 'Hidden' : (job.salary_min ? `${job.currency} ${(job.salary_min / 1000).toFixed(0)}k - ${(job.salary_max / 1000).toFixed(0)}k` : 'Not Specified')}
                            <span className="text-sm font-normal text-slate-500 ml-1">/year</span>
                        </p>
                        <p className="text-sm text-slate-500 mt-2 flex items-center gap-1.5">
                            {job.hide_salary ? <AlertTriangle className="w-4 h-4 text-orange-500" /> : <CheckCircle className="w-4 h-4 text-green-500" />}
                            {job.hide_salary ? 'Hidden from candidates' : 'Visible to candidates'}
                        </p>
                    </div>

                    <div className="bg-slate-800 text-white p-5 rounded-xl border border-slate-700">
                        <p className="text-xs font-bold text-slate-400 uppercase tracking-wider mb-2">Market Analysis</p>
                        <p className="text-sm text-slate-300">Average for this role in {job.city || 'location'}:</p>
                        <p className="text-lg font-bold mt-1 text-emerald-400">
                            {job.currency || '$'} 65,000 - 85,000
                        </p>
                        <div className="mt-3 pt-3 border-t border-slate-700">
                            <p className="text-sm font-medium text-emerald-400 flex items-center gap-1.5">
                                ✓ Competitive (+18% above market)
                            </p>
                        </div>
                    </div>
                </div>

                {benefits.length > 0 && (
                    <div className="bg-white p-5 rounded-xl border border-slate-200">
                        <p className="text-xs font-bold text-slate-400 uppercase tracking-wider mb-3">Benefits & Perks</p>
                        <div className="flex flex-wrap gap-2 text-sm">
                            {benefits.map((b: string, i: number) => (
                                <span key={i} className="px-3 py-1.5 bg-slate-50 text-slate-700 border border-slate-200 rounded-full flex items-center gap-1.5">
                                    <CheckCircle className="w-3.5 h-3.5 text-slate-400" /> {b}
                                </span>
                            ))}
                        </div>
                    </div>
                )}
            </section>

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

            {/* SECTION 6: Application Details */}
            <section className="space-y-4">
                <h3 className="text-lg font-bold text-slate-900">Application Details</h3>
                <div className="bg-white border text-sm text-slate-700 border-slate-200 rounded-xl overflow-hidden divide-y divide-slate-100">
                    <div className="p-4 flex items-start gap-4">
                        <div className="w-10 h-10 rounded-full bg-blue-50 flex items-center justify-center shrink-0">
                            <Clock className="w-5 h-5 text-blue-600" />
                        </div>
                        <div>
                            <p className="font-semibold text-slate-900">Application Deadline</p>
                            <p className="text-slate-600 mt-0.5">30 days from approval</p>
                            <p className="text-xs font-medium text-green-600 mt-1">✓ Reasonable timeframe</p>
                        </div>
                    </div>
                    <div className="p-4 flex items-start gap-4">
                        <div className="w-10 h-10 rounded-full bg-slate-50 flex items-center justify-center shrink-0">
                            <CheckCircle className="w-5 h-5 text-slate-600" />
                        </div>
                        <div>
                            <p className="font-semibold text-slate-900">Application Method</p>
                            <p className="text-slate-600 mt-0.5">Apply through platform</p>
                            <p className="text-xs font-medium text-green-600 mt-1">✓ Preferred (Trackable, secure)</p>
                        </div>
                    </div>
                </div>
            </section>

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

            {/* SECTION 7: Company Profile Snippet */}
            <section className="bg-slate-800 rounded-2xl p-6 text-white text-sm">
                <div className="flex justify-between items-start mb-6">
                    <div className="flex items-center gap-3">
                        <div className="w-12 h-12 rounded-lg bg-white/10 flex items-center justify-center font-bold text-xl">
                            {job.company?.charAt(0)}
                        </div>
                        <div>
                            <h3 className="font-bold text-base">{job.company}</h3>
                            <a href="#" className="text-blue-400 hover:text-blue-300 flex items-center gap-1 mt-0.5 text-xs">
                                www.{job.company?.toLowerCase().replace(/\s/g, '')}.com <ExternalLink className="w-3 h-3" />
                            </a>
                        </div>
                    </div>
                    <div className="bg-green-500/20 text-green-400 border border-green-500/30 px-3 py-1.5 rounded-lg text-center">
                        <p className="text-xs font-semibold uppercase tracking-wider mb-0.5 text-green-500/70">Trust Score</p>
                        <p className="font-bold text-lg">8.5<span className="text-xs text-green-500/50">/10</span></p>
                    </div>
                </div>

                <div className="grid grid-cols-2 gap-x-8 gap-y-4 text-slate-300">
                    <div>
                        <p className="text-slate-500 font-medium mb-1 text-xs uppercase">History</p>
                        <p>15 Jobs Posted</p>
                        <p>14 Approved (93%)</p>
                    </div>
                    <div>
                        <p className="text-slate-500 font-medium mb-1 text-xs uppercase">Verification</p>
                        <p className="flex items-center gap-1.5"><CheckCircle className="w-3.5 h-3.5 text-green-400" /> Email Domain</p>
                        <p className="flex items-center gap-1.5"><CheckCircle className="w-3.5 h-3.5 text-green-400" /> Active Website</p>
                    </div>
                </div>
            </section>
        </div>
    );
}
