'use client';

import { useEffect, useState } from 'react';
import { useRouter } from 'next/navigation';
import Sidebar from './Sidebar';

export default function AdminLayout({ children }) {
  const router = useRouter();
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    const token = localStorage.getItem('adminToken');
    console.log('[LAYOUT] Checking token:', token ? 'exists' : 'missing');
    if (!token) {
      console.log('[LAYOUT] No token, redirecting to login');
      window.location.href = '/admin/login';
    } else {
      console.log('[LAYOUT] Token found, allowing access');
      setLoading(false);
    }
  }, [router]);

  if (loading) {
    return (
      <div style={{
        display: 'flex',
        alignItems: 'center',
        justifyContent: 'center',
        minHeight: '100vh',
        color: 'var(--text-muted)'
      }}>
        Loading...
      </div>
    );
  }

  return (
    <div style={{ display: 'flex' }}>
      <Sidebar />
      <main style={{
        marginLeft: '240px',
        flex: 1,
        minHeight: '100vh',
        padding: '32px',
      }}>
        {children}
      </main>
    </div>
  );
}
