import { NavLink } from 'react-router-dom'
import {
  LayoutDashboard, Globe, BarChart2, FlaskConical,
  History, Calendar, TrendingUp, Zap, DollarSign, Settings, BrainCircuit, Activity, BookOpen, FileBarChart, Brain, ShieldAlert, Microscope, ScrollText, Gauge, GitCompare, Building2, Users, ScanEye, CandlestickChart, PlayCircle, Radio, Bot, Sliders, Waves, Layers, History as HistoryIcon
} from 'lucide-react'
import { useGeoRiskScore, useAiStatus, usePortfolioSummary } from '../../hooks/useApi'
import clsx from 'clsx'

const nav = [
  { to: '/', icon: LayoutDashboard, label: 'Cockpit' },
  { to: '/geo', icon: Globe, label: 'Geopolitical Radar' },
  { to: '/markets', icon: BarChart2, label: 'Markets & Prices' },
  { to: '/macro', icon: Activity, label: 'Macro Regime' },
  { to: '/options', icon: TrendingUp, label: 'Options Lab' },
  { to: '/strategy-builder', icon: Layers, label: 'Strategy Builder' },
  { to: '/saxo-history', icon: HistoryIcon, label: 'Saxo History' },
  { to: '/wavelets-simulation', icon: Waves, label: 'Wavelets Simulation' },
  { to: '/patterns', icon: Zap, label: 'Patterns' },
  { to: '/pattern-lab', icon: FlaskConical, label: 'Pattern Lab' },
  { to: '/portfolio', icon: DollarSign, label: 'Portfolio' },
  { to: '/journal', icon: BookOpen, label: 'Trading Journal' },
  { to: '/rapport', icon: FileBarChart, label: 'Cycle Report' },
  { to: '/super-contexte', icon: Brain, label: 'Super Context' },
  { to: '/analytics', icon: FlaskConical, label: 'Analytics' },
  { to: '/analytics-advanced', icon: Microscope, label: 'Advanced Analytics' },
  { to: '/risk', icon: ShieldAlert, label: 'Risk Dashboard' },
  { to: '/var', icon: Gauge, label: 'VaR Analysis' },
  { to: '/position-history', icon: GitCompare, label: 'Position History' },
  { to: '/backtest', icon: History, label: 'Backtest' },
  { to: '/calendar', icon: Calendar, label: 'Calendar' },
  { to: '/simulator', icon: Sliders, label: 'EUR/USD Simulator' },
  { to: '/causal-lab', icon: FlaskConical, label: 'Lab Causal' },
  { to: '/instrument-models', icon: Microscope, label: 'Modèles Instruments' },
  { to: '/macro-series', icon: TrendingUp, label: 'Macro Series' },
  { to: '/institutional', icon: Building2, label: 'Inst. Reports' },
  { to: '/specialist-desks', icon: Users, label: 'Specialist Desks' },
  { to: '/instruments', icon: CandlestickChart, label: 'Instrument Analysis' },
  { to: '/market-events', icon: Radio, label: 'Market Events' },
  { to: '/ai-desks', icon: Bot, label: 'AI Desks' },
  { to: '/cycle-actions', icon: PlayCircle, label: 'Cycle Actions' },
  { to: '/snapshot', icon: ScanEye, label: 'Snapshot Externe' },
  { to: '/logs', icon: ScrollText, label: 'System Logs' },
  { to: '/config', icon: Settings, label: 'Configuration' },
]

const riskColors: Record<string, string> = {
  low: 'text-emerald-400 bg-emerald-900/30 border-emerald-700/40',
  medium: 'text-yellow-400 bg-yellow-900/30 border-yellow-700/40',
  high: 'text-orange-400 bg-orange-900/30 border-orange-700/40',
  extreme: 'text-red-400 bg-red-900/30 border-red-700/40 animate-pulse',
}

export default function Sidebar() {
  const { data: riskScore } = useGeoRiskScore()
  const { data: aiStatus } = useAiStatus()
  const { data: summary } = usePortfolioSummary()

  return (
    <aside className="w-56 bg-dark-800 border-r border-slate-700/40 flex flex-col h-screen sticky top-0">
      {/* Logo */}
      <div className="p-4 border-b border-slate-700/40">
        <div className="flex items-center gap-2">
          <Zap className="w-5 h-5 text-blue-400" />
          <div>
            <div className="text-sm font-bold text-white tracking-wide">OpenFin</div>
            <div className="text-xs text-slate-500">Intelligence v2.0</div>
          </div>
        </div>
      </div>

      {/* Geo risk indicator */}
      {riskScore && (
        <div className={clsx('mx-3 mt-3 px-3 py-2 rounded border text-xs', riskColors[riskScore.level])}>
          <div className="font-semibold uppercase tracking-wider">Geo Risk</div>
          <div className="text-lg font-bold">{riskScore.score}/100</div>
          <div className="capitalize">{riskScore.level}</div>
        </div>
      )}

      {/* Portfolio mini summary */}
      {summary && (summary.open_positions > 0 || summary.unrealized_pnl !== 0) && (
        <div className="mx-3 mt-2 px-3 py-2 rounded border border-slate-700/30 bg-dark-700/50 text-xs">
          <div className="text-slate-500 uppercase tracking-wider text-xs mb-1">Portfolio</div>
          <div className="flex justify-between">
            <span className="text-slate-400">{summary.open_positions} positions</span>
            <span className={clsx('font-bold', summary.unrealized_pnl >= 0 ? 'text-emerald-400' : 'text-red-400')}>
              {summary.unrealized_pnl >= 0 ? '+' : ''}{summary.unrealized_pnl?.toFixed(0)}€
            </span>
          </div>
        </div>
      )}

      {/* Navigation */}
      <nav className="flex-1 p-2 mt-2 space-y-0.5 overflow-y-auto">
        {nav.map(({ to, icon: Icon, label }) => (
          <NavLink
            key={to}
            to={to}
            end={to === '/'}
            className={({ isActive }) => clsx('nav-link', isActive && 'active')}
          >
            <Icon className="w-4 h-4 shrink-0" />
            <span>{label}</span>
          </NavLink>
        ))}
      </nav>

      {/* AI status badge */}
      <div className="px-3 py-2 border-t border-slate-700/40">
        <div className={clsx('flex items-center gap-2 text-xs px-2 py-1.5 rounded', {
          'bg-blue-900/30 text-blue-400': aiStatus?.enabled,
          'bg-dark-700 text-slate-600': !aiStatus?.enabled,
        })}>
          <BrainCircuit className="w-3.5 h-3.5" />
          <span>{aiStatus?.enabled ? 'AI GPT-4o active' : 'AI not configured'}</span>
        </div>
      </div>

      {/* Footer */}
      <div className="p-3 border-t border-slate-700/40 text-xs text-slate-600">
        <div>© 2026 OpenFin</div>
        <div className="text-slate-700 mt-0.5">Local · IBKR ready</div>
      </div>
    </aside>
  )
}
