"use client"

import { Suspense, type ReactNode } from "react"

import { AppSidebar } from "@/components/app-sidebar"
import { AuthGuard } from "@/components/auth/auth-guard"
import { SidebarInset, SidebarProvider } from "@/components/ui/sidebar"
import { Skeleton } from "@/components/ui/skeleton"
import { useAuth } from "@/lib/auth-client"

interface AppShellProps {
  header: ReactNode
  children: ReactNode
}

export function AppShell({ header, children }: AppShellProps) {
  const { isLoading } = useAuth()
  return (
    <SidebarProvider>
      <AppSidebar />
      <SidebarInset>
        <Suspense fallback={null}>
          <AuthGuard />
        </Suspense>
        {isLoading ? (
          <div className="px-4 pt-4 lg:px-6">
            <div className="flex items-center justify-between gap-4">
              <Skeleton className="h-7 w-48" />
              <div className="hidden items-center gap-2 sm:flex">
                <Skeleton className="h-9 w-28" />
                <Skeleton className="h-9 w-28" />
              </div>
            </div>
            <Skeleton className="mt-2 h-4 w-72" />
          </div>
        ) : (
          header
        )}
        <main className="flex flex-1 flex-col gap-8 bg-gradient-to-br from-background via-background to-primary/10 pb-12 pt-6">
          {isLoading ? (
            <div className="space-y-6">
              <div className="px-4 lg:px-6">
                <div className="grid gap-6 lg:grid-cols-2">
                  <Skeleton className="h-56 w-full rounded-xl" />
                  <Skeleton className="h-56 w-full rounded-xl" />
                </div>
              </div>
              <div className="px-4 lg:px-6">
                <Skeleton className="h-64 w-full rounded-xl" />
              </div>
            </div>
          ) : (
            children
          )}
        </main>
      </SidebarInset>
    </SidebarProvider>
  )
}
