"use client";

import React from "react";
import Sidebar from "./_components/sidebar";
import AdminRouteGuard from "@/shared/components/auth/admin-route-guard";
import { useAuthSession } from "@/shared/hooks/use-session";
import { Loader2 } from "lucide-react";
import RefDataBootstrap from "./_components/refdata-bootstrap";

interface AdminLayoutProps {
  children: React.ReactNode;
}

const AdminLayout: React.FC<AdminLayoutProps> = ({ children }) => {
  const { isLoading } = useAuthSession();

  if (isLoading) {
    return (
      <div className="flex items-center justify-center min-h-screen">
        <div className="flex flex-col items-center space-y-2">
          <Loader2 className="h-8 w-8 animate-spin text-blue-500" />
          <p className="text-sm text-gray-500">Memuat sesi...</p>
        </div>
      </div>
    );
  }

  return (
    <AdminRouteGuard>
      <div className="flex h-screen p-4 space-x-4 overflow-hidden">
        <Sidebar />

        <div className="flex flex-col flex-1 overflow-hidden">
          {/* Prefetch reference data for selects across admin */}
          <RefDataBootstrap />
          <main className="flex-1 overflow-y-auto rounded-lg border border-zinc-200 relative pb-6">{children}</main>
        </div>
      </div>
    </AdminRouteGuard>
  );
};

export default AdminLayout;
