"use client";

import { useEffect } from "react";
import { motion } from "motion/react";
import { Sidebar } from "./sidebar";
import { PageToolbar } from "./page-toolbar";
import { PageTransition } from "./page-transition";
import AuthGuard from "@/components/auth/auth-guard";
import { springSoft } from "@/lib/motion";

interface DashboardLayoutProps {
  children: React.ReactNode;
}

export function DashboardLayout({ children }: DashboardLayoutProps) {
  // Lock document scroll so only <main> scrolls (avoids double scrollbars).
  useEffect(() => {
    const html = document.documentElement;
    const { body } = document;
    const prev = {
      htmlOverflow: html.style.overflow,
      bodyOverflow: body.style.overflow,
    };

    html.style.overflow = "hidden";
    body.style.overflow = "hidden";

    return () => {
      html.style.overflow = prev.htmlOverflow;
      body.style.overflow = prev.bodyOverflow;
    };
  }, []);

  return (
    <AuthGuard>
      <div className="h-screen overflow-hidden bg-background">
        <motion.div
          initial={{ opacity: 0 }}
          animate={{ opacity: 1 }}
          transition={springSoft}
          className="flex h-full min-h-0"
        >
          <Sidebar />
          <div className="flex min-h-0 min-w-0 flex-1 flex-col bg-background">
            <PageToolbar />
            <main className="min-h-0 flex-1 overflow-y-auto overscroll-y-contain">
              <PageTransition>{children}</PageTransition>
            </main>
          </div>
        </motion.div>
      </div>
    </AuthGuard>
  );
}
