"use client";

import { useState, useTransition } from "react";
import { completeOnboarding } from "@/app/actions/auth";
import { CheckCircle, MapPin, Briefcase, Globe, Users, ArrowRight, ArrowLeft, Loader2, Building2 } from "lucide-react";

const STEPS = [
    { id: 1, label: "Company", icon: "🏢" },
    { id: 2, label: "Details", icon: "📋" },
    { id: 3, label: "Hiring", icon: "🎯" },
];

const COMPANY_SIZES = ["1–10", "11–50", "51–200", "201–500", "500–1000", "1000+"];
const INDUSTRIES = ["Technology", "Finance", "Healthcare", "Education", "Marketing", "E-commerce", "Manufacturing", "Consulting", "Media", "Other"];

interface Props { userId: string }

export default function EmployerOnboardingWizard({ userId }: Props) {
    const [step, setStep] = useState(1);
    const [isPending, startTransition] = useTransition();

    // Step 1 — Company Info
    const [companyName, setCompanyName] = useState("");
    const [companyWebsite, setCompanyWebsite] = useState("");
    const [companyLocation, setCompanyLocation] = useState("");

    // Step 2 — Company Details
    const [industry, setIndustry] = useState("");
    const [companySize, setCompanySize] = useState("");
    const [companyDescription, setCompanyDescription] = useState("");

    // Step 3 — Hiring Needs
    const [hiringFor, setHiringFor] = useState<string[]>([]);
    const [hiringInput, setHiringInput] = useState("");

    const addRole = (r: string) => {
        const val = r.trim();
        if (val && !hiringFor.includes(val) && hiringFor.length < 10) {
            setHiringFor([...hiringFor, val]);
            setHiringInput("");
        }
    };

    const handleFinish = () => {
        const fd = new FormData();
        fd.append("userId", userId);
        fd.append("role", "employer");
        fd.append("companyName", companyName);
        fd.append("companyWebsite", companyWebsite);
        fd.append("companyLocation", companyLocation);
        fd.append("industry", industry);
        fd.append("companySize", companySize);
        fd.append("companyDescription", companyDescription);
        fd.append("hiringFor", JSON.stringify(hiringFor));
        startTransition(() => { completeOnboarding(fd); });
    };

    return (
        <div className="px-4 pb-16 pt-4 max-w-2xl mx-auto">
            {/* Progress */}
            <div className="mb-10">
                <div className="flex items-center justify-between mb-4">
                    {STEPS.map((s, i) => (
                        <div key={s.id} className="flex items-center">
                            <div className="flex items-center gap-2">
                                <div className={`w-9 h-9 rounded-full flex items-center justify-center text-sm font-bold transition-all duration-300 ${
                                    step > s.id
                                        ? "bg-emerald-500 text-white shadow-lg shadow-emerald-500/30"
                                        : step === s.id
                                        ? "bg-purple-500 text-white shadow-lg shadow-purple-500/30"
                                        : "bg-white/10 text-slate-500"
                                }`}>
                                    {step > s.id ? <CheckCircle className="w-5 h-5" /> : s.icon}
                                </div>
                                <span className={`text-sm font-medium hidden sm:block ${step === s.id ? "text-white" : "text-slate-500"}`}>
                                    {s.label}
                                </span>
                            </div>
                            {i < STEPS.length - 1 && (
                                <div className="w-16 sm:w-24 h-px mx-3 bg-white/10 relative">
                                    <div
                                        className="absolute inset-y-0 left-0 bg-purple-500 transition-all duration-500"
                                        style={{ width: step > s.id ? "100%" : "0%" }}
                                    />
                                </div>
                            )}
                        </div>
                    ))}
                </div>
                <div className="text-center">
                    <p className="text-slate-400 text-sm">Step {step} of {STEPS.length}</p>
                </div>
            </div>

            <div className="bg-white/5 backdrop-blur-xl border border-white/10 rounded-2xl shadow-2xl p-8">

                {/* STEP 1 — Company Info */}
                {step === 1 && (
                    <div className="space-y-6">
                        <div>
                            <h2 className="text-2xl font-bold text-white mb-1">Tell us about your company</h2>
                            <p className="text-slate-400 text-sm">Help candidates learn about where they&apos;d be working</p>
                        </div>

                        <div>
                            <label className="block text-sm font-medium text-slate-300 mb-1.5">
                                <Building2 className="w-4 h-4 inline mr-1" />
                                Company Name <span className="text-red-400">*</span>
                            </label>
                            <input
                                type="text"
                                value={companyName}
                                onChange={(e) => setCompanyName(e.target.value)}
                                className="w-full rounded-xl bg-white/5 border border-white/10 px-4 py-3 text-white placeholder-slate-500 focus:border-purple-500/60 focus:ring-2 focus:ring-purple-500/20 outline-none transition-all text-sm"
                                placeholder="Acme Corporation"
                            />
                        </div>

                        <div>
                            <label className="block text-sm font-medium text-slate-300 mb-1.5">
                                <Globe className="w-4 h-4 inline mr-1" />
                                Company Website
                            </label>
                            <input
                                type="url"
                                value={companyWebsite}
                                onChange={(e) => setCompanyWebsite(e.target.value)}
                                className="w-full rounded-xl bg-white/5 border border-white/10 px-4 py-3 text-white placeholder-slate-500 focus:border-purple-500/60 focus:ring-2 focus:ring-purple-500/20 outline-none transition-all text-sm"
                                placeholder="https://yourcompany.com"
                            />
                        </div>

                        <div>
                            <label className="block text-sm font-medium text-slate-300 mb-1.5">
                                <MapPin className="w-4 h-4 inline mr-1" />
                                Headquarters Location
                            </label>
                            <input
                                type="text"
                                value={companyLocation}
                                onChange={(e) => setCompanyLocation(e.target.value)}
                                className="w-full rounded-xl bg-white/5 border border-white/10 px-4 py-3 text-white placeholder-slate-500 focus:border-purple-500/60 focus:ring-2 focus:ring-purple-500/20 outline-none transition-all text-sm"
                                placeholder="Lagos, Nigeria"
                            />
                        </div>
                    </div>
                )}

                {/* STEP 2 — Details */}
                {step === 2 && (
                    <div className="space-y-6">
                        <div>
                            <h2 className="text-2xl font-bold text-white mb-1">Company details</h2>
                            <p className="text-slate-400 text-sm">Help us match you with the right candidates</p>
                        </div>

                        <div>
                            <label className="block text-sm font-medium text-slate-300 mb-3">
                                <Briefcase className="w-4 h-4 inline mr-1" />
                                Industry
                            </label>
                            <div className="flex flex-wrap gap-2">
                                {INDUSTRIES.map((ind) => (
                                    <button
                                        key={ind}
                                        type="button"
                                        onClick={() => setIndustry(ind)}
                                        className={`px-4 py-2 rounded-xl text-sm font-medium border-2 transition-all duration-200 ${
                                            industry === ind
                                                ? "border-purple-500 bg-purple-500/15 text-purple-300"
                                                : "border-white/10 bg-white/5 text-slate-400 hover:border-white/25 hover:text-slate-300"
                                        }`}
                                    >
                                        {industry === ind ? "✓ " : ""}{ind}
                                    </button>
                                ))}
                            </div>
                        </div>

                        <div>
                            <label className="block text-sm font-medium text-slate-300 mb-3">
                                <Users className="w-4 h-4 inline mr-1" />
                                Company Size (employees)
                            </label>
                            <div className="grid grid-cols-3 gap-2">
                                {COMPANY_SIZES.map((size) => (
                                    <button
                                        key={size}
                                        type="button"
                                        onClick={() => setCompanySize(size)}
                                        className={`py-2.5 rounded-xl text-sm font-medium border-2 transition-all duration-200 ${
                                            companySize === size
                                                ? "border-purple-500 bg-purple-500/15 text-purple-300"
                                                : "border-white/10 bg-white/5 text-slate-400 hover:border-white/25 hover:text-slate-300"
                                        }`}
                                    >
                                        {size}
                                    </button>
                                ))}
                            </div>
                        </div>

                        <div>
                            <label className="block text-sm font-medium text-slate-300 mb-1.5">
                                About the Company <span className="text-slate-600 font-normal">(optional)</span>
                            </label>
                            <textarea
                                value={companyDescription}
                                onChange={(e) => setCompanyDescription(e.target.value)}
                                rows={4}
                                maxLength={600}
                                className="w-full rounded-xl bg-white/5 border border-white/10 px-4 py-3 text-white placeholder-slate-500 focus:border-purple-500/60 focus:ring-2 focus:ring-purple-500/20 outline-none transition-all text-sm resize-none"
                                placeholder="Describe your company culture, mission, and what makes it a great place to work..."
                            />
                            <p className="text-xs text-slate-600 mt-1 text-right">{companyDescription.length}/600</p>
                        </div>
                    </div>
                )}

                {/* STEP 3 — Hiring */}
                {step === 3 && (
                    <div className="space-y-6">
                        <div>
                            <h2 className="text-2xl font-bold text-white mb-1">What roles are you hiring for?</h2>
                            <p className="text-slate-400 text-sm">Help us surface the right candidates for your needs</p>
                        </div>

                        <div>
                            <label className="block text-sm font-medium text-slate-300 mb-1.5">Add job titles you&apos;re hiring for</label>
                            <div className="flex gap-2">
                                <input
                                    type="text"
                                    value={hiringInput}
                                    onChange={(e) => setHiringInput(e.target.value)}
                                    onKeyDown={(e) => { if (e.key === "Enter") { e.preventDefault(); addRole(hiringInput); } }}
                                    className="flex-1 rounded-xl bg-white/5 border border-white/10 px-4 py-3 text-white placeholder-slate-500 focus:border-purple-500/60 focus:ring-2 focus:ring-purple-500/20 outline-none transition-all text-sm"
                                    placeholder="e.g. Frontend Engineer"
                                />
                                <button
                                    type="button"
                                    onClick={() => addRole(hiringInput)}
                                    className="px-4 py-3 rounded-xl bg-purple-500/20 border border-purple-500/30 text-purple-300 hover:bg-purple-500/30 transition-colors text-sm font-medium"
                                >
                                    Add
                                </button>
                            </div>
                        </div>

                        {hiringFor.length > 0 && (
                            <div className="flex flex-wrap gap-2">
                                {hiringFor.map((r) => (
                                    <span key={r} className="inline-flex items-center gap-1.5 px-3 py-1.5 rounded-lg bg-purple-500/15 border border-purple-500/30 text-purple-300 text-sm">
                                        {r}
                                        <button type="button" onClick={() => setHiringFor(hiringFor.filter((x) => x !== r))} className="hover:text-red-400 transition-colors">×</button>
                                    </span>
                                ))}
                            </div>
                        )}

                        <div className="p-4 rounded-xl bg-purple-500/10 border border-purple-500/20">
                            <div className="flex items-center gap-3">
                                <CheckCircle className="w-6 h-6 text-purple-400 flex-shrink-0" />
                                <div>
                                    <p className="text-sm font-semibold text-white">Ready to start hiring!</p>
                                    <p className="text-xs text-slate-400 mt-0.5">Post your first job right after setup. All details can be edited any time.</p>
                                </div>
                            </div>
                        </div>
                    </div>
                )}

                {/* Navigation */}
                <div className="flex items-center justify-between mt-8 pt-6 border-t border-white/10">
                    <button
                        type="button"
                        onClick={() => setStep((s) => Math.max(1, s - 1))}
                        disabled={step === 1}
                        className="flex items-center gap-2 px-5 py-2.5 rounded-xl text-slate-400 hover:text-white border border-white/10 hover:border-white/20 transition-all text-sm disabled:opacity-30 disabled:cursor-not-allowed"
                    >
                        <ArrowLeft className="w-4 h-4" /> Back
                    </button>

                    {step < STEPS.length ? (
                        <button
                            type="button"
                            onClick={() => { if (step === 1 && !companyName.trim()) return; setStep((s) => s + 1); }}
                            disabled={step === 1 && !companyName.trim()}
                            className="flex items-center gap-2 px-6 py-2.5 rounded-xl bg-purple-600 text-white font-semibold hover:bg-purple-700 shadow-lg shadow-purple-500/30 transition-all text-sm group disabled:opacity-50"
                        >
                            Continue <ArrowRight className="w-4 h-4 group-hover:translate-x-0.5 transition-transform" />
                        </button>
                    ) : (
                        <button
                            type="button"
                            onClick={handleFinish}
                            disabled={isPending}
                            className="flex items-center gap-2 px-6 py-2.5 rounded-xl bg-emerald-500 text-white font-semibold hover:bg-emerald-600 shadow-lg shadow-emerald-500/30 transition-all text-sm"
                        >
                            {isPending ? <Loader2 className="w-4 h-4 animate-spin" /> : <><CheckCircle className="w-4 h-4" /> Complete Setup</>}
                        </button>
                    )}
                </div>
            </div>
        </div>
    );
}
