import type { ReactNode } from "react";

import { cookies } from "next/headers";

import { AppSidebar } from "@/app/(main)/dashboard/_components/sidebar/app-sidebar";
import { AuthGuard } from "@/components/auth-guard";
import { CloudBackground } from "@/components/silisuite/cloud-background";
import { Separator } from "@/components/ui/separator";
import { SidebarInset, SidebarProvider, SidebarTrigger } from "@/components/ui/sidebar";
import { SIDEBAR_COLLAPSIBLE_VALUES, SIDEBAR_VARIANT_VALUES } from "@/lib/preferences/layout";
import { cn } from "@/lib/utils";
import { getPreference } from "@/server/server-actions";

import { SearchDialog } from "./_components/sidebar/search-dialog";
import { ThemeSwitcher } from "./_components/sidebar/theme-switcher";

export default async function Layout({ children }: Readonly<{ children: ReactNode }>) {
  const cookieStore = await cookies();
  const defaultOpen = cookieStore.get("sidebar_state")?.value !== "false";
  const [variant, collapsible] = await Promise.all([
    getPreference("sidebar_variant", SIDEBAR_VARIANT_VALUES, "inset"),
    getPreference("sidebar_collapsible", SIDEBAR_COLLAPSIBLE_VALUES, "icon"),
  ]);

  return (
    <AuthGuard>
      <div className="relative min-h-screen">
        <CloudBackground />
        <div className="relative z-10">
          <SidebarProvider
            defaultOpen={defaultOpen}
            style={
              {
                "--sidebar-width": "calc(var(--spacing) * 68)",
              } as React.CSSProperties
            }
          >
            <AppSidebar variant={variant} collapsible={collapsible} />
            <SidebarInset
              className={cn(
                "[html[data-content-layout=centered]_&>*]:mx-auto",
                "[html[data-content-layout=centered]_&>*]:w-full",
                "[html[data-content-layout=centered]_&>*]:max-w-screen-2xl",
                "peer-data-[variant=inset]:border",
              )}
            >
              <header
                className={cn(
                  "ss-chrome-header flex h-12 shrink-0 items-center gap-2 border-b transition-[width,height] duration-200 ease-out group-has-data-[collapsible=icon]/sidebar-wrapper:h-12",
                  "[html[data-navbar-style=sticky]_&]:sticky [html[data-navbar-style=sticky]_&]:top-0 [html[data-navbar-style=sticky]_&]:z-50 [html[data-navbar-style=sticky]_&]:overflow-hidden [html[data-navbar-style=sticky]_&]:rounded-t-[inherit]",
                )}
              >
                <div className="flex w-full items-center justify-between px-3 sm:px-4 lg:px-5">
                  <div className="flex items-center gap-1 lg:gap-2">
                    <SidebarTrigger className="-ml-1" />
                    <Separator
                      orientation="vertical"
                      className="mx-2 data-[orientation=vertical]:h-4 data-[orientation=vertical]:self-center"
                    />
                    <SearchDialog />
                  </div>
                  <div className="flex items-center gap-2">
                    <ThemeSwitcher />
                  </div>
                </div>
              </header>
              <div className="h-full w-full px-3 py-4 sm:px-4 sm:py-5">{children}</div>
            </SidebarInset>
          </SidebarProvider>
        </div>
      </div>
    </AuthGuard>
  );
}
