import { getCurrentUser } from "@/lib/auth";
import { redirect } from "next/navigation";
import prisma from "@/lib/prisma";
import { CompanyProfileForm } from "./_components/CompanyProfileForm";
import { calculateEmployerCompletion } from "@/lib/profile-completion";
import { Building, ArrowLeft } from "lucide-react";
import Link from "next/link";

export const dynamic = "force-dynamic";

export default async function EmployerCompanyPage() {
    const user = await getCurrentUser();
    if (!user) redirect("/login");
    if (user.role !== "employer") redirect("/dashboard");

    const profile = await prisma.profile.findUnique({ where: { user_id: user.id } });
    const completion = calculateEmployerCompletion(profile, !!profile?.company_logo_url);

    return (
        <div className="max-w-3xl mx-auto space-y-6 pb-12">
            {/* Header */}
            <div className="flex items-center gap-3">
                <Link href="/dashboard" className="flex items-center gap-1.5 text-sm text-slate-500 hover:text-slate-900 transition-colors">
                    <ArrowLeft className="w-4 h-4" /> Dashboard
                </Link>
            </div>

            <div className="flex items-center justify-between">
                <div className="flex items-center gap-3">
                    <div className="w-10 h-10 rounded-xl bg-indigo-100 flex items-center justify-center">
                        <Building className="w-5 h-5 text-indigo-600" />
                    </div>
                    <div>
                        <h1 className="text-2xl font-bold text-slate-900">Company Profile</h1>
                        <p className="text-sm text-slate-500">How candidates see your company</p>
                    </div>
                </div>

                {/* Completion ring */}
                <div className="flex items-center gap-3">
                    <div className="text-right">
                        <p className="text-2xl font-black text-slate-900">{completion.score}%</p>
                        <p className="text-xs text-slate-500">Complete</p>
                    </div>
                    <svg className="w-12 h-12 transform -rotate-90">
                        <circle cx="24" cy="24" r="20" className="text-slate-100" strokeWidth="5" fill="none" stroke="currentColor" />
                        <circle cx="24" cy="24" r="20" className="text-indigo-500"
                            strokeWidth="5" fill="none" stroke="currentColor"
                            strokeDasharray={`${2 * Math.PI * 20}`}
                            strokeDashoffset={`${2 * Math.PI * 20 * (1 - completion.score / 100)}`}
                            strokeLinecap="round"
                        />
                    </svg>
                </div>
            </div>

            {/* Missing tips */}
            {completion.missing.length > 0 && (
                <div className="bg-amber-50 border border-amber-200 rounded-xl p-4">
                    <p className="text-sm font-semibold text-amber-800 mb-2">To improve your profile:</p>
                    <ul className="space-y-1">
                        {completion.missing.slice(0, 3).map((item) => (
                            <li key={item} className="text-sm text-amber-700 flex items-center gap-2">
                                <span className="w-1.5 h-1.5 rounded-full bg-amber-500 shrink-0" />
                                {item}
                            </li>
                        ))}
                    </ul>
                </div>
            )}

            <CompanyProfileForm profile={profile} />
        </div>
    );
}
