'use client';

import Image from 'next/image';
import { useGameStore, type PlayerGauges } from '@/stores/gameStore';
import { t } from '@/lib/i18n';
import { Bell, Settings } from 'lucide-react';
import { LanguageSelector } from '../ui/LanguageSelector';
import { GaugeBar } from '../ui/GaugeBar';
import { GAUGE_ICONS } from '../ui/GameIcons';

/** Typed gauge configuration — no `as any` needed */
interface GaugeConfig {
  storeKey: keyof PlayerGauges;
  color: string;
}

const GAUGE_CONFIG: GaugeConfig[] = [
  { storeKey: 'fortune', color: '#d4a843' },
  { storeKey: 'pouvoir', color: '#e63946' },
  { storeKey: 'chaleur', color: '#f97316' },
  { storeKey: 'conscience', color: '#8b5cf6' },
  { storeKey: 'respect', color: '#d4a843' },
  { storeKey: 'social', color: '#06b6d4' },
  { storeKey: 'popularite', color: '#eab308' },
  { storeKey: 'instabilite', color: '#ef4444' },
  { storeKey: 'influence', color: '#22c55e' },
  { storeKey: 'ecologie', color: '#16a34a' },
  { storeKey: 'sante_physique', color: '#ec4899' },
];

export function Header() {
  const { player, gauges, isAuthenticated } = useGameStore();

  return (
    <header
      className="w-full flex items-center justify-between px-4 shrink-0"
      style={{
        height: 'var(--header-height)',
        background: 'linear-gradient(180deg, var(--bg-header) 0%, var(--bg-primary) 100%)',
        borderBottom: '1px solid var(--border-dark)',
      }}
    ><meta charset="utf-8">
      {/* Left: Logo */}
      <div className="flex items-center gap-3 shrink-0">
        <a href="/" className="flex items-center gap-2">
          <Image src="/images/logo.png" alt="Ombres & Lumières" width={44} height={44} priority />
          <span className="font-gothic text-gold-gradient text-lg hidden md:block">
            Ombres & Lumières
          </span>
        </a>
      </div>

      {/* Center: Gauges (only when authenticated & in game) */}
      {isAuthenticated && gauges && (
        <div className="flex items-center gap-1 overflow-x-auto mx-4 flex-1 justify-center">
          {GAUGE_CONFIG.map((g) => {
            const value = gauges[g.storeKey] ?? 0;
            const max = g.storeKey === 'fortune' ? 100000 : 100;
            const IconComponent = GAUGE_ICONS[g.storeKey];

            return (
              <GaugeBar
                key={g.storeKey}
                label={
                  IconComponent ? (
                    <IconComponent size={14} color={g.color} />
                  ) : (
                    g.storeKey
                  )
                }
                value={value}
                max={max}
                color={g.color}
                tooltip={t(`gauges.${g.storeKey}`)}
              />
            );
          })}
        </div>
      )}

      {/* Right: Actions */}
      <div className="flex items-center gap-3 shrink-0">
        {isAuthenticated && (
          <>
            <span className="text-xs hidden lg:block" style={{ color: 'var(--text-muted)' }}>
              12:00
            </span>
            <button className="relative p-1.5 rounded hover:bg-[var(--bg-hover)] transition-colors">
              <Bell size={18} style={{ color: 'var(--text-secondary)' }} />
            </button>
            <button className="p-1.5 rounded hover:bg-[var(--bg-hover)] transition-colors">
              <Settings size={18} style={{ color: 'var(--text-secondary)' }} />
            </button>
          </>
        )}
        <LanguageSelector />
        {isAuthenticated && player && (
          <div className="flex items-center gap-2 ml-2">
            <div
              className="w-8 h-8 rounded-full flex items-center justify-center text-xs font-bold"
              style={{ background: 'var(--color-gold-dark)', color: 'var(--color-black)' }}
            >
              {(player.character_name || player.username).charAt(0).toUpperCase()}
            </div>
            <span className="text-sm hidden xl:block max-w-[120px] truncate">
              {player.character_name || player.username}
            </span>
          </div>
        )}
      </div>
    </header>
  );
}
