import React, { useEffect } from 'react';
import {
    SidebarInset,
    SidebarProvider,
    SidebarTrigger,
} from '@/components/ui/sidebar';
import { AppSidebar } from '@/components/app-sidebar';
import { Head, router, usePage } from '@inertiajs/react';
import { PageProps } from '@/types/type';
import { toast, Toaster } from '@/components/ui/toast';
import { Separator } from '@/components/ui/separator';
import { Button } from '@/components/ui/button';
import { LogOut } from 'lucide-react';
import { ThemeProvider } from '@/components/theme-provider';
import { ModeToggle } from './mode-toggle';

export default function Layout({
    children,
    title,
}: {
    children: React.ReactNode;
    title: string;
}) {
    // show error
    const { flash } = usePage<PageProps>().props;
    useEffect(() => {
        if (flash?.error) {
            toast.add({
                type: 'error',
                description: flash.error,
                priority: 'high',
            });
        }

        if (flash?.success) {
            toast.add({
                type: 'success',
                description: flash.success,
                priority: 'high',
            });
        }
    }, [flash?.id]);
    return (
        <ThemeProvider defaultTheme="light" storageKey="vite-ui-theme">
            <SidebarProvider
                style={
                    {
                        '--sidebar-width': 'calc(var(--spacing) * 72)',
                        '--header-height': 'calc(var(--spacing) * 12)',
                    } as React.CSSProperties
                }
            >
                <AppSidebar variant="inset" />

                <SidebarInset>
                    <Toaster />
                    <Head title={title} />
                    <header className="flex h-(--header-height) shrink-0 items-center gap-2 border-b py-7 shadow-none transition-[width,height] ease-linear group-has-data-[collapsible=icon]/sidebar-wrapper:h-(--header-height) print:hidden">
                        <div className="flex w-full items-center gap-1 px-4 lg:gap-2 lg:px-6">
                            <SidebarTrigger className="-ml-1" />
                            <h1 className="text-base font-medium">{title}</h1>
                        </div>
                    </header>

                    <div className="flex flex-1 flex-col">
                        <div className="@container/main flex flex-1 flex-col gap-2 p-5 md:p-10">
                            {children}
                        </div>
                    </div>

                    <div className="px-4 py-3 text-right print:hidden">
                        <small>
                            Development By{' '}
                            <a
                                href="https://www.facebook.com/Ctg.it.soft.bd/"
                                target="_blank"
                                className="underline"
                            >
                                Ctgitsoft
                            </a>
                            {'  |  '}
                            <a
                                href="https://www.facebook.com/codesuab/"
                                target="_blank"
                                className="underline"
                            >
                                Codesuab
                            </a>
                        </small>
                    </div>
                </SidebarInset>
            </SidebarProvider>
        </ThemeProvider>
    );
}
