'use client';

import { useEffect, useState } from 'react';
import { useRouter } from 'next/navigation';
import { DashboardSidebar } from './DashboardSidebar';
import { DashboardHeader } from './DashboardHeader';
import { useI18n } from '@/lib/i18n';

export function DashboardLayoutClient({ children }: { children: React.ReactNode }) {
  const router = useRouter();
  const { t } = useI18n();
  const [mounted, setMounted] = useState(false);
  const [isAuthenticated, setIsAuthenticated] = useState(false);

  useEffect(() => {
    setMounted(true);
    const token = localStorage.getItem('token');
    if (!token) {
      router.push('/auth/login?redirect=/dashboard');
    } else {
      setIsAuthenticated(true);
    }
  }, [router]);

  if (!mounted || !isAuthenticated) {
    return (
      <div className="flex h-screen items-center justify-center bg-brand-bg dark:bg-[#0a0a0a]">
        <div className="text-center space-y-4">
          <div className="animate-spin rounded-full h-8 w-8 border-4 border-brand-muted border-t-brand-accent mx-auto"></div>
          <p className="text-xs font-semibold uppercase tracking-[0.25em] text-brand-dark/50 dark:text-white/50" role="status">
            {t('dashboard.loading') || 'Chargement…'}
          </p>
        </div>
      </div>
    );
  }

  return (
    <div className="flex h-screen bg-brand-bg dark:bg-[#0a0a0a]">
      <DashboardSidebar />
      <div className="flex flex-1 flex-col overflow-hidden">
        <DashboardHeader />
        <main className="flex-1 overflow-y-auto">{children}</main>
      </div>
    </div>
  );
}
