"use client";

import { JobPostingFormValues } from "@/lib/validations/job";
import { MapPin, Briefcase, DollarSign, Clock, ArrowLeft, Send } from "lucide-react";
import DOMPurify from 'dompurify';

interface JobPreviewProps {
    formData: JobPostingFormValues;
}

export function JobPreview({ formData }: JobPreviewProps) {
    // Safe default extractors
    const hasSalary = !formData.hide_salary && formData.salary_min;
    const locationLabel = formData.work_location_type === "remote"
        ? "Remote"
        : formData.city
            ? formData.city + (formData.state ? `, ${formData.state}` : "")
            : "Location TBD";

    return (
        <div className="space-y-4 animate-in fade-in slide-in-from-bottom-4 duration-500">
            <div className="flex items-center justify-between mb-8">
                <div>
                    <h2 className="text-2xl font-bold text-slate-900">Preview Job Posting</h2>
                    <p className="text-slate-500 mt-1">This is exactly how candidates will see your listing.</p>
                </div>
            </div>

            {/* The Preview Card (Mirrors the job/[id]/page.tsx visual layout) */}
            <div className="border-4 border-slate-100 rounded-3xl overflow-hidden shadow-lg shadow-slate-200/50 bg-white">

                {/* Header Ribbon */}
                <div className="bg-slate-900 p-2 text-center">
                    <span className="text-xs font-bold tracking-widest text-slate-300 uppercase">Live Preview Mode</span>
                </div>

                <div className="bg-gradient-to-r from-blue-50 to-purple-50 p-8">
                    <div className="flex h-16 w-16 items-center justify-center rounded-xl bg-white text-2xl font-bold text-slate-500 shadow-sm uppercase">
                        {formData.company ? formData.company.charAt(0) : "?"}
                    </div>
                    <h1 className="mt-6 text-3xl font-bold text-slate-900">{formData.title || "Job Title"}</h1>
                    <p className="mt-2 text-lg text-slate-600">
                        {formData.company || "Company Name"} &bull; <span className="text-slate-500">{locationLabel}</span>
                    </p>
                </div>

                <div className="flex flex-wrap items-center justify-between gap-6 px-8 py-6 border-b border-slate-100">
                    <div className="flex flex-wrap gap-6 text-sm font-medium text-slate-600">
                        <span className="flex items-center gap-2 capitalize">
                            <Briefcase className="h-5 w-5 text-slate-400" /> {formData.type?.replace("-", " ") || "Full-time"}
                        </span>
                        <span className="flex items-center gap-2">
                            <DollarSign className="h-5 w-5 text-slate-400" />
                            {hasSalary
                                ? `${formData.salary_min?.toLocaleString()} ${formData.salary_max ? `- ${formData.salary_max.toLocaleString()}` : ''} ${formData.currency || 'USD'}`
                                : 'Competitive Pay'}
                        </span>
                        <span className="flex items-center gap-2">
                            <Clock className="h-5 w-5 text-slate-400" /> Posted Just Now
                        </span>
                    </div>

                    <button
                        disabled
                        className="flex items-center gap-2 rounded-xl bg-primary px-8 py-3 font-semibold text-white opacity-50 cursor-not-allowed"
                    >
                        Apply Now <Send className="h-4 w-4" />
                    </button>
                </div>

                <div className="grid gap-8 lg:grid-cols-3 p-8 bg-slate-50">
                    <div className="lg:col-span-2 space-y-8">
                        <section className="rounded-xl border border-slate-100 bg-white p-8 shadow-sm">
                            <h2 className="mb-4 text-xl font-bold text-slate-900">Description</h2>
                            {formData.description ? (
                                <div
                                    className="prose prose-slate max-w-none text-slate-600"
                                    dangerouslySetInnerHTML={{ __html: DOMPurify.sanitize(formData.description) }}
                                />
                            ) : (
                                <p className="text-slate-400 italic">No description provided...</p>
                            )}
                        </section>

                        <section className="rounded-xl border border-slate-100 bg-white p-8 shadow-sm">
                            <h2 className="mb-4 text-xl font-bold text-slate-900">Responsibilities</h2>
                            <ul className="list-disc space-y-2 pl-5 text-slate-600">
                                {formData.responsibilities?.filter(r => r.trim() !== "").map((resp, i) => (
                                    <li key={i}>{resp}</li>
                                )) || <li className="text-slate-400 italic">No responsibilities added...</li>}
                            </ul>
                        </section>

                        <section className="rounded-xl border border-slate-100 bg-white p-8 shadow-sm">
                            <h2 className="mb-4 text-xl font-bold text-slate-900">Requirements</h2>
                            <ul className="list-disc space-y-2 pl-5 text-slate-600">
                                {formData.required_qualifications?.filter(r => r.trim() !== "").map((req, i) => (
                                    <li key={i}>{req}</li>
                                )) || <li className="text-slate-400 italic">No requirements added...</li>}
                            </ul>
                        </section>
                    </div>

                    <div className="space-y-6">
                        <section className="rounded-xl border border-slate-100 bg-white p-6 shadow-sm">
                            <h3 className="mb-4 font-bold text-slate-900">Required Skills</h3>
                            <div className="flex flex-wrap gap-2">
                                {formData.skills?.map(skill => (
                                    <span key={skill.name} className="rounded-lg bg-primary/10 px-3 py-1 text-sm font-medium text-primary">
                                        {skill.name}
                                    </span>
                                )) || <span className="text-sm text-slate-400 italic">No skills added...</span>}
                            </div>
                        </section>

                        {formData.benefits && formData.benefits.length > 0 && (
                            <section className="rounded-xl border border-slate-100 bg-white p-6 shadow-sm">
                                <h3 className="mb-4 font-bold text-slate-900">Benefits</h3>
                                <div className="flex flex-col gap-2">
                                    {formData.benefits.map(benefit => (
                                        <span key={benefit} className="flex items-center gap-2 text-sm text-slate-600">
                                            <div className="w-1.5 h-1.5 rounded-full bg-emerald-500" />
                                            {benefit}
                                        </span>
                                    ))}
                                </div>
                            </section>
                        )}

                        <section className="rounded-xl border border-slate-100 bg-white p-6 shadow-sm">
                            <h3 className="mb-4 font-bold text-slate-900">About {formData.company || "the Company"}</h3>
                            <p className="text-sm leading-relaxed text-slate-600">
                                {formData.summary || "A brief overview of the company and role."}
                            </p>
                        </section>
                    </div>
                </div>

            </div>
        </div>
    );
}
