"use client"
// app/dashboard/page.tsx
import { redirect } from "next/navigation"
import { useRouter } from "next/navigation";
import { useEffect } from "react";
import { useUser } from "../user-context";

export default function Dashboard() {

    const { isAuthenticated, isLoading, user } = useUser()
    const router = useRouter();

    useEffect(() => {
        if (!isLoading) {
            if (!isAuthenticated) {
                router.push('/login');
                return;
            }
        } else {
            if (!isAuthenticated) {
                router.push("/");
                return;
            }

            if (!user?.permissionPairs?.some(pair => pair.resource === "admin" && pair.action === "write")) {
                router.push("/");
                return;
            }
        }
    }, [isAuthenticated, isLoading, router, user]);

    redirect('/admin/dashboard')

}
