"use client"

import { useEffect, useState } from "react"
import { useRouter } from "next/navigation"
import { getCurrentUser } from "@/lib/auth"
import { AdminDashboardContent } from "@/components/dashboard-contents/admin-dashboard"
import { CompanyDashboardContent } from "@/components/dashboard-contents/company-dashboard"
import { CandidateDashboardContent } from "@/components/dashboard-contents/candidate-dashboard"

export default function DashboardPage() {
    const router = useRouter()
    const [user, setUser] = useState(getCurrentUser())
    const [loading, setLoading] = useState(true)

    useEffect(() => {
        const currentUser = getCurrentUser()
        if (!currentUser) {
            router.push("/login")
            return
        }
        setUser(currentUser)
        setLoading(false)
    }, [router])

    if (loading) {
        return null
    }

    if (!user) return null

    // Role-based rendering
    if (user.role === "admin" || user.roles?.includes("superadmin")) {
        return <AdminDashboardContent />
    }

    if (user.role === "company" || user.roles?.includes("companyAdmin")) {
        return <CompanyDashboardContent />
    }

    if (user.role === "candidate" || user.role === "jobSeeker") {
        return <CandidateDashboardContent />
    }

    // Fallback
    return (
        <div className="p-8 text-center">
            <h1 className="text-2xl font-bold">Acesso não configurado</h1>
            <p>Seu perfil não possui um dashboard associado.</p>
        </div>
    )
}
