"use client"

import { useEffect, useState } from "react"
import { useRouter } from "next/navigation"
import { getCurrentUser, isAdminUser } 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 (isAdminUser(user)) {
        return <AdminDashboardContent />
    }

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

    if (user.role === "candidate" || user.roles?.includes("candidate")) {
        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>
    )
}
