import { db } from "@/lib/db";
import { notFound } from "next/navigation";
import { ProfileHeader } from "@/components/profile/ProfileHeader";
import { getCurrentUser } from "@/lib/auth";
import Link from "next/link";
import { ArrowLeft, Briefcase, Building2, Users, Globe } from "lucide-react";
import { JobCard } from "@/components/jobs/JobCard";

interface EmployerProfilePageProps {
    params: Promise<{ id: string }>;
}

export default async function EmployerProfilePage(props: EmployerProfilePageProps) {
    const params = await props.params;
    const database = await db.read();
    const user = database.users.find(u => u.id === params.id && u.role === 'employer');

    if (!user) {
        notFound();
    }

    // Check if this is the current user's profile
    const currentUser = await getCurrentUser();
    const isOwnProfile = currentUser?.id === user.id;

    // Get jobs posted by this employer
    const employerJobs = database.jobs.filter(job => job.company === user.company);

    return (
        <div className="min-h-screen bg-slate-50/50 py-8">
            <div className="container mx-auto px-4 max-w-5xl">
                {/* Back Button */}
                <Link href="/dashboard" className="inline-flex items-center gap-2 text-sm font-medium text-slate-600 hover:text-primary mb-6 transition-colors">
                    <ArrowLeft className="h-4 w-4" />
                    Back to Dashboard
                </Link>

                {/* Profile Header */}
                <ProfileHeader user={user} isOwnProfile={isOwnProfile} />

                {/* Company Stats */}
                <div className="grid grid-cols-1 md:grid-cols-3 gap-4 mt-8">
                    {user.companyIndustry && (
                        <div className="bg-white rounded-xl border border-slate-100 p-6 shadow-sm">
                            <div className="flex items-center gap-3">
                                <div className="w-12 h-12 rounded-full bg-primary/10 flex items-center justify-center">
                                    <Building2 className="h-6 w-6 text-primary" />
                                </div>
                                <div>
                                    <p className="text-sm text-slate-500">Industry</p>
                                    <p className="text-lg font-bold text-slate-900">{user.companyIndustry}</p>
                                </div>
                            </div>
                        </div>
                    )}

                    {user.companySize && (
                        <div className="bg-white rounded-xl border border-slate-100 p-6 shadow-sm">
                            <div className="flex items-center gap-3">
                                <div className="w-12 h-12 rounded-full bg-accent/10 flex items-center justify-center">
                                    <Users className="h-6 w-6 text-accent" />
                                </div>
                                <div>
                                    <p className="text-sm text-slate-500">Company Size</p>
                                    <p className="text-lg font-bold text-slate-900">{user.companySize}</p>
                                </div>
                            </div>
                        </div>
                    )}

                    <div className="bg-white rounded-xl border border-slate-100 p-6 shadow-sm">
                        <div className="flex items-center gap-3">
                            <div className="w-12 h-12 rounded-full bg-emerald-50 flex items-center justify-center">
                                <Briefcase className="h-6 w-6 text-emerald-600" />
                            </div>
                            <div>
                                <p className="text-sm text-slate-500">Open Positions</p>
                                <p className="text-lg font-bold text-slate-900">{employerJobs.length}</p>
                            </div>
                        </div>
                    </div>
                </div>

                {/* About Section */}
                {user.companyDescription && (
                    <section className="mt-8">
                        <div className="bg-white rounded-2xl border border-slate-100 p-8 shadow-sm">
                            <h2 className="text-2xl font-bold text-slate-900 mb-4">About {user.company}</h2>
                            <p className="text-slate-700 leading-relaxed whitespace-pre-line">{user.companyDescription}</p>
                        </div>
                    </section>
                )}

                {/* Open Positions */}
                {employerJobs.length > 0 && (
                    <section className="mt-8">
                        <h2 className="text-2xl font-bold text-slate-900 mb-6">Open Positions</h2>
                        <div className="space-y-4">
                            {employerJobs.map(job => (
                                <JobCard
                                    key={job.id}
                                    {...job}
                                    isSaved={false}
                                />
                            ))}
                        </div>
                    </section>
                )}
            </div>
        </div>
    );
}
