import { db } from "@/lib/db";
import { notFound, redirect } from "next/navigation";
import { ProfileHeader } from "@/components/profile/ProfileHeader";
import { ExperienceTimeline } from "@/components/profile/ExperienceTimeline";
import { EducationList } from "@/components/profile/EducationList";
import { getCurrentUser } from "@/lib/auth";
import Link from "next/link";
import { ArrowLeft } from "lucide-react";

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

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

    if (!user) {
        notFound();
    }

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

    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} />

                <div className="grid grid-cols-1 lg:grid-cols-3 gap-6 mt-8">
                    {/* Left Column - Skills */}
                    <div className="lg:col-span-1">
                        <div className="bg-white rounded-2xl border border-slate-100 p-6 shadow-sm">
                            <h2 className="text-lg font-bold text-slate-900 mb-4">Skills</h2>
                            {user.skills && user.skills.length > 0 ? (
                                <div className="flex flex-wrap gap-2">
                                    {user.skills.map((skill, index) => (
                                        <span key={index} className="px-3 py-1.5 text-sm font-medium rounded-lg bg-primary/10 text-primary border border-primary/20">
                                            {skill}
                                        </span>
                                    ))}
                                </div>
                            ) : (
                                <p className="text-sm text-slate-500">No skills listed</p>
                            )}
                        </div>
                    </div>

                    {/* Right Column - Experience & Education */}
                    <div className="lg:col-span-2 space-y-8">
                        {/* Experience Section */}
                        <section>
                            <h2 className="text-2xl font-bold text-slate-900 mb-6">Experience</h2>
                            <ExperienceTimeline experiences={user.experience || []} />
                        </section>

                        {/* Education Section */}
                        <section>
                            <h2 className="text-2xl font-bold text-slate-900 mb-6">Education</h2>
                            <EducationList education={user.education || []} />
                        </section>
                    </div>
                </div>
            </div>
        </div>
    );
}
