'use client';

import { useGameStore } from '@/stores/gameStore';
import { t } from '@/lib/i18n';
import {
  Wallet, Store, Shield, MapPin, Home, Car, Warehouse
} from 'lucide-react';

const TABS = [
  { id: 'finances', icon: Wallet, labelKey: 'sidebar.finances' },
  { id: 'businesses', icon: Store, labelKey: 'sidebar.businesses' },
  { id: 'team', icon: Shield, labelKey: 'sidebar.team' },
  { id: 'territory', icon: MapPin, labelKey: 'sidebar.territory' },
  { id: 'real_estate', icon: Home, labelKey: 'sidebar.realEstate' },
  { id: 'vehicles', icon: Car, labelKey: 'sidebar.vehicles' },
  { id: 'warehouse', icon: Warehouse, labelKey: 'sidebar.warehouse' },
] as const;

export function SidebarRight() {
  const { activeSidebarRightTab, setSidebarRightTab, uiVisibility, balances } = useGameStore();

  return (
    <div className="flex flex-col h-full">
      {/* Tab navigation */}
      <div
        className="flex border-b shrink-0 overflow-x-auto"
        style={{ borderColor: 'var(--border-dark)' }}
      >
        {TABS.map((tab) => {
          
          
          if (tab.id !== 'finances') { const vk = tab.id as keyof typeof uiVisibility; if (vk in uiVisibility && !uiVisibility[vk]) return null; }

          const Icon = tab.icon;
          const isActive = activeSidebarRightTab === tab.id;

          return (
            <button
              key={tab.id}
              onClick={() => setSidebarRightTab(tab.id)}
              className="flex-1 flex flex-col items-center gap-0.5 py-2 px-1 text-[10px] transition-colors relative"
              style={{
                color: isActive ? 'var(--color-gold)' : 'var(--text-muted)',
                background: isActive ? 'var(--bg-tertiary)' : 'transparent',
              }}
              title={t(tab.labelKey)}
            >
              <Icon size={16} />
              <span className="truncate w-full text-center">{t(tab.labelKey)}</span>
              {isActive && (
                <div
                  className="absolute bottom-0 left-0 right-0 h-[2px]"
                  style={{ background: 'var(--color-gold)' }}
                />
              )}
            </button>
          );
        })}
      </div>

      {/* Tab content */}
      <div className="flex-1 overflow-y-auto p-3">
        {activeSidebarRightTab === 'finances' && (
          <div className="space-y-3">
            <h3 className="font-impact text-sm" style={{ color: 'var(--color-gold)' }}>
              {t('sidebar.finances')}
            </h3>
            <div className="space-y-2">
              <FinanceRow label="∞ Light" value={balances?.infinite_light ?? 0} />
              <FinanceRow label="∞ Shadow" value={balances?.infinite_shadow ?? 0} color="var(--color-red)" />
              <FinanceRow label="ᛞ Dagaz" value={balances?.dagaz ?? 0} color="var(--color-gold)" />
              <FinanceRow label="⚵ Quint." value={balances?.quintessence ?? 0} color="#8b5cf6" />
            </div>
            <div
              className="mt-3 pt-3 border-t flex justify-between text-sm font-bold"
              style={{ borderColor: 'var(--border-dark)' }}
            >
              <span>{t('gauges.fortune')}</span>
              <span style={{ color: 'var(--color-gold)' }}>
                ∞ {((balances?.infinite_light ?? 0) + (balances?.infinite_shadow ?? 0)).toLocaleString()}
              </span>
            </div>
          </div>
        )}

        {activeSidebarRightTab !== 'finances' && (
          <div className="text-center py-8">
            <p className="text-xs" style={{ color: 'var(--text-muted)' }}>
              {t(`sidebar.${activeSidebarRightTab === 'real_estate' ? 'realEstate' : activeSidebarRightTab}`)}
            </p>
          </div>
        )}
      </div>
    </div>
  );
}

function FinanceRow({ label, value, color }: { label: string; value: number; color?: string }) {
  return (
    <div className="flex justify-between items-center text-xs">
      <span style={{ color: 'var(--text-secondary)' }}>{label}</span>
      <span className="font-mono font-bold" style={{ color: color || 'var(--text-primary)' }}>
        {value.toLocaleString()}
      </span>
    </div>
  );
}
