'use client';

import { useGameStore } from '@/stores/gameStore';
import { Header } from './Header';
import { SidebarLeft } from './SidebarLeft';
import { SidebarRight } from './SidebarRight';
import { Footer } from './Footer';
import { PhoneOverlay } from '../ui/Phone';
import { ErrorLightbox } from '../ui/ErrorLightbox';
import { TutorialManager } from '../tutorial/TutorialManager';
import { ExpandableBlock } from '../ui/ExpandableBlock';
import { AkashaBanner } from '../ui/AkashaBanner';

interface GameLayoutProps {
  children: React.ReactNode;
}

/**
 * Game Layout — grid aligned with the v2 mockup (vue-normale.html).
 *
 * ┌──────────────────────────── Header (56px) ────────────────────────────┐
 * │                         Akasha signature (28px)                         │
 * ├─── 300px ──┬──────── 1fr ────────┬──── 320px ────┐ padding 14, gap 14  │
 * │  Sidebar   │       Main map      │   Sidebar     │                     │
 * │  (panel)   │     (panel+map)     │   (panel)     │                     │
 * ├────────────┴─────────────────────┴───────────────┤                     │
 * │            Footer — 5-col panel (208px)          │ margin 0 14 14 14   │
 * └──────────────────────────────────────────────────┘
 *
 * All panels share the same rounded/gold-inset treatment via the
 * `panel-rounded` utility class in globals.css — takes the stylistic
 * foundation straight from the validated mockup while preserving every
 * existing component (Leaflet map + POIs, gameplay data, real gang/mission
 * logic) untouched.
 */
export function GameLayout({ children }: GameLayoutProps) {
  const { uiVisibility } = useGameStore();

  return (
    <div
      className="h-screen overflow-hidden game-shell"
      style={{
        background: 'var(--bg-primary)',
        backgroundImage: "url('https://mafiaz.world/images/Mafiaz-World-Ombres-et-Lumieres-Bandeau_facebook.png')",
        backgroundSize: 'cover',
        backgroundPosition: 'top center',
        backgroundRepeat: 'no-repeat',
        backgroundAttachment: 'fixed',
        display: 'grid',
        gridTemplateRows: 'var(--header-height) auto 1fr var(--footer-height)',
      }}
    >
      {/* HEADER */}
      <Header />

      {/* Akasha Production signature banner — slim ad-slot-style between
          header and the game middle. */}
      <AkashaBanner />

      {/* MIDDLE: 3-col grid with 14px gutter + 14px side padding. */}
      <section
        className="game-middle"
        style={{
          display: 'grid',
          gridTemplateColumns: uiVisibility.sidebar_left && uiVisibility.sidebar_right
            ? '300px 1fr 320px'
            : uiVisibility.sidebar_left
              ? '300px 1fr'
              : uiVisibility.sidebar_right
                ? '1fr 320px'
                : '1fr',
          gap: 14,
          padding: '14px 14px 8px 14px',
          minHeight: 0,
          position: 'relative',
          zIndex: 1,
        }}
      >
        {/* SIDEBAR LEFT */}
        {uiVisibility.sidebar_left && (
          <aside className="panel-rounded panel-scroll relative">
            <ExpandableBlock label="Profil & attributs" className="h-full" buttonOffset={{ top: 10, right: 10 }}>
              <SidebarLeft />
            </ExpandableBlock>
          </aside>
        )}

        {/* MAIN CENTER — map lives inside a rounded panel. */}
        <main className="panel-rounded relative overflow-hidden" style={{ minHeight: 0 }}>
          {children}
        </main>

        {/* SIDEBAR RIGHT */}
        {uiVisibility.sidebar_right && (
          <aside className="panel-rounded panel-scroll relative">
            <ExpandableBlock label="Finances, parc & rivaux" className="h-full" buttonOffset={{ top: 10, right: 10 }}>
              <SidebarRight />
            </ExpandableBlock>
          </aside>
        )}
      </section>

      {/* FOOTER — rounded panel container, its inner 5-col grid is handled
          inside Footer.tsx. Side + bottom margin so the panel visually floats. */}
      <div style={{ margin: '0 14px 14px 14px', minHeight: 0 }} className="panel-rounded overflow-hidden">
        <Footer />
      </div>

      {/* Overlays */}
      <PhoneOverlay />
      <ErrorLightbox />
      <TutorialManager />
    </div>
  );
}
