"use client";

import { setUserRole } from "@/app/actions/auth";
import { getCurrentUser } from "@/lib/auth";
import { useEffect, useState, useTransition } from "react";
import { Briefcase, User, ArrowRight, Loader2 } from "lucide-react";

export default function RoleSelectionPage() {
    const [userId, setUserId] = useState<string | null>(null);
    const [selected, setSelected] = useState<"candidate" | "employer">("candidate");
    const [isPending, startTransition] = useTransition();

    // We need the userId from server. Use a hidden endpoint or pass via cookie
    // For simplicity, we fetch from a lightweight client-side API call
    useEffect(() => {
        fetch("/api/me")
            .then((r) => r.json())
            .then((d) => setUserId(d?.id ?? null));
    }, []);

    const handleSubmit = () => {
        if (!userId) return;
        const fd = new FormData();
        fd.append("role", selected);
        fd.append("userId", userId);
        startTransition(() => { setUserRole(fd); });
    };

    return (
        <div className="min-h-screen flex items-center justify-center bg-[#0a0f1e] relative overflow-hidden p-4">
            <div className="absolute -top-40 -right-40 w-[600px] h-[600px] rounded-full bg-[#0070f3]/20 blur-[120px]" />
            <div className="absolute -bottom-40 -left-40 w-[600px] h-[600px] rounded-full bg-purple-600/15 blur-[120px]" />
            <div className="absolute inset-0 bg-[linear-gradient(rgba(255,255,255,0.02)_1px,transparent_1px),linear-gradient(90deg,rgba(255,255,255,0.02)_1px,transparent_1px)] bg-[size:64px_64px] pointer-events-none" />

            <div className="relative z-10 w-full max-w-lg">
                <div className="text-center mb-10">
                    <div className="inline-flex items-center justify-center w-14 h-14 rounded-2xl bg-[#0070f3] shadow-lg shadow-[#0070f3]/30 mb-4">
                        <svg className="w-8 h-8 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M13 10V3L4 14h7v7l9-11h-7z" />
                        </svg>
                    </div>
                    <h1 className="text-3xl font-extrabold text-white mb-3">Welcome to ProtenConnect!</h1>
                    <p className="text-slate-400">One quick thing — what brings you here today?</p>
                </div>

                <div className="bg-white/5 backdrop-blur-xl border border-white/10 rounded-2xl shadow-2xl p-8">
                    <div className="grid grid-cols-2 gap-4 mb-8">
                        <button
                            onClick={() => setSelected("candidate")}
                            className={`relative group flex flex-col items-center gap-4 p-6 rounded-xl border-2 transition-all duration-300 ${
                                selected === "candidate"
                                    ? "border-[#0070f3] bg-[#0070f3]/10 shadow-lg shadow-[#0070f3]/20"
                                    : "border-white/10 bg-white/5 hover:border-white/25 hover:bg-white/8"
                            }`}
                        >
                            {selected === "candidate" && (
                                <div className="absolute top-3 right-3 w-5 h-5 rounded-full bg-[#0070f3] flex items-center justify-center">
                                    <svg className="w-3 h-3 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={3} d="M5 13l4 4L19 7" />
                                    </svg>
                                </div>
                            )}
                            <div className={`w-14 h-14 rounded-xl flex items-center justify-center transition-colors ${
                                selected === "candidate" ? "bg-[#0070f3]/20" : "bg-white/8"
                            }`}>
                                <User className={`w-7 h-7 ${selected === "candidate" ? "text-[#0070f3]" : "text-slate-400"}`} />
                            </div>
                            <div className="text-center">
                                <p className="font-bold text-white text-lg">Candidate</p>
                                <p className="text-sm text-slate-400 mt-1 leading-tight">I&apos;m looking for a job or career opportunity</p>
                            </div>
                        </button>

                        <button
                            onClick={() => setSelected("employer")}
                            className={`relative group flex flex-col items-center gap-4 p-6 rounded-xl border-2 transition-all duration-300 ${
                                selected === "employer"
                                    ? "border-purple-500 bg-purple-500/10 shadow-lg shadow-purple-500/20"
                                    : "border-white/10 bg-white/5 hover:border-white/25 hover:bg-white/8"
                            }`}
                        >
                            {selected === "employer" && (
                                <div className="absolute top-3 right-3 w-5 h-5 rounded-full bg-purple-500 flex items-center justify-center">
                                    <svg className="w-3 h-3 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={3} d="M5 13l4 4L19 7" />
                                    </svg>
                                </div>
                            )}
                            <div className={`w-14 h-14 rounded-xl flex items-center justify-center transition-colors ${
                                selected === "employer" ? "bg-purple-500/20" : "bg-white/8"
                            }`}>
                                <Briefcase className={`w-7 h-7 ${selected === "employer" ? "text-purple-400" : "text-slate-400"}`} />
                            </div>
                            <div className="text-center">
                                <p className="font-bold text-white text-lg">Employer</p>
                                <p className="text-sm text-slate-400 mt-1 leading-tight">I&apos;m hiring and want to post jobs</p>
                            </div>
                        </button>
                    </div>

                    <button
                        onClick={handleSubmit}
                        disabled={isPending || !userId}
                        className={`w-full flex items-center justify-center gap-2 py-4 rounded-xl font-bold text-white shadow-lg transition-all duration-200 disabled:opacity-50 group text-base ${
                            selected === "employer"
                                ? "bg-purple-600 hover:bg-purple-700 shadow-purple-500/30"
                                : "bg-[#0070f3] hover:bg-[#0060d3] shadow-[#0070f3]/30"
                        }`}
                    >
                        {isPending ? (
                            <Loader2 className="w-5 h-5 animate-spin" />
                        ) : (
                            <>
                                Continue as {selected === "employer" ? "Employer" : "Candidate"}
                                <ArrowRight className="w-5 h-5 group-hover:translate-x-0.5 transition-transform" />
                            </>
                        )}
                    </button>
                </div>
            </div>
        </div>
    );
}
