import { getProfile } from "@/lib/data"
import { createClient } from "@/lib/supabase-server"
import { redirect } from "next/navigation"
import { Sidebar } from "@/components/dashboard/sidebar"
import { DashboardHeader } from "@/components/dashboard/header"

import { AutoLogoutHandler } from "@/components/dashboard/auto-logout-handler"

export default async function DashboardLayout({
    children,
}: Readonly<{
    children: React.ReactNode;
}>) {
    const supabase = createClient()
    const { data: { user } } = await supabase.auth.getUser()

    if (!user) {
        redirect("/login")
    }

    const profile = await getProfile(user.id)

    return (
        <div className="flex min-h-screen w-full flex-col bg-muted/40">
            <AutoLogoutHandler />
            <aside className="fixed inset-y-0 left-0 z-10 hidden w-64 flex-col border-r bg-background sm:flex">
                <div className="flex h-14 items-center border-b px-4 lg:h-[60px] lg:px-6">
                    <span className="font-semibold text-lg">ParaKasa Panel</span>
                </div>
                <Sidebar className="flex-1" />
            </aside>
            <div className="flex flex-col sm:gap-4 sm:py-4 sm:pl-64">
                <DashboardHeader user={user} profile={profile} />
                <main className="grid flex-1 items-start gap-4 p-4 sm:px-6 sm:py-0 md:gap-8">
                    {children}
                </main>
            </div>
        </div>
    );
}
