import { BrowserRouter, Routes, Route, useLocation, useNavigate, useParams, Navigate } from 'react-router-dom'
import { useEffect, ComponentType } from 'react'
import Sidebar from './components/layout/Sidebar'
import TabBar from './components/layout/TabBar'
import { TabsProvider, useTabs } from './context/TabsContext'
import { KEEP_ALIVE_META } from './config/keepAliveMeta'
import Dashboard from './pages/Dashboard'
import GeoRadar from './pages/GeoRadar'
import Markets from './pages/Markets'
import MacroRegime from './pages/MacroRegime'
import OptionsLab from './pages/OptionsLab'
import StrategyBuilder from './pages/StrategyBuilder'
import SaxoHistory from './pages/SaxoHistory'
import WaveletsSimulation from './pages/WaveletsSimulation'
import Backtest from './pages/Backtest'
import CalendarPage from './pages/CalendarPage'
import Portfolio from './pages/Portfolio'
import PatternEditor from './pages/PatternEditor'
import PatternExplorer from './pages/PatternExplorer'
import PatternLab from './pages/PatternLab'
import JournalDeBord from './pages/JournalDeBord'
import RapportIA from './pages/RapportIA'
import SuperContexte from './pages/SuperContexte'
import Config from './pages/Config'
import Analytics from './pages/Analytics'
import AnalyticsAdvanced from './pages/AnalyticsAdvanced'
import RiskDashboard from './pages/RiskDashboard'
import SystemLogs from './pages/SystemLogs'
import VaRAnalysis from './pages/VaRAnalysis'
import PositionHistory from './pages/PositionHistory'
import InstitutionalReports from './pages/InstitutionalReports'
import SpecialistDesks from './pages/SpecialistDesks'
import ExternalSnapshot from './pages/ExternalSnapshot'
import InstrumentDashboard from './pages/InstrumentDashboard'
import CycleActions from './pages/CycleActions'
import MarketEvents from './pages/MarketEvents'
import AIDesks from './pages/AIDesks'
import MacroSeriesPage from './pages/MacroSeriesPage'
import EuroSimulator from './pages/EuroSimulator'
import CausalLab from './pages/CausalLab'
import InstrumentModels from './pages/InstrumentModels'
import ChatWidget from './components/ChatWidget'
import { useCycleWatcher } from './hooks/useApi'

// ── Keep-alive pages ──────────────────────────────────────────────────────────
// All pages except InstrumentDashboard (uses URL :id param — would break when hidden).
// Order must match KEEP_ALIVE_META in keepAliveMeta.ts.

const KEEP_ALIVE_DEFS: { path: string; component: ComponentType }[] = [
  { path: '/',                   component: Dashboard          },
  { path: '/geo',                component: GeoRadar           },
  { path: '/markets',            component: Markets            },
  { path: '/macro',              component: MacroRegime        },
  { path: '/options',            component: OptionsLab         },
  { path: '/strategy-builder',   component: StrategyBuilder    },
  { path: '/saxo-history',       component: SaxoHistory        },
  { path: '/wavelets-simulation', component: WaveletsSimulation },
  { path: '/patterns',           component: PatternExplorer    },
  { path: '/pattern-lab',        component: PatternLab         },
  { path: '/portfolio',          component: Portfolio          },
  { path: '/journal',            component: JournalDeBord      },
  { path: '/rapport',            component: RapportIA          },
  { path: '/super-contexte',     component: SuperContexte      },
  { path: '/analytics',          component: Analytics          },
  { path: '/analytics-advanced', component: AnalyticsAdvanced  },
  { path: '/risk',               component: RiskDashboard      },
  { path: '/var',                component: VaRAnalysis        },
  { path: '/position-history',   component: PositionHistory    },
  { path: '/backtest',           component: Backtest           },
  { path: '/calendar',           component: CalendarPage       },
  { path: '/simulator',          component: EuroSimulator      },
  { path: '/causal-lab',         component: CausalLab          },
  { path: '/instrument-models',  component: InstrumentModels   },
  { path: '/macro-series',       component: MacroSeriesPage    },
  { path: '/institutional',      component: InstitutionalReports },
  { path: '/specialist-desks',   component: SpecialistDesks    },
  { path: '/market-events',      component: MarketEvents       },
  { path: '/ai-desks',           component: AIDesks            },
  { path: '/cycle-actions',      component: CycleActions       },
  { path: '/snapshot',           component: ExternalSnapshot   },
  { path: '/logs',               component: SystemLogs         },
  { path: '/config',             component: Config             },
  { path: '/patterns/edit',      component: PatternEditor      },
]

const KEEP_ALIVE_PATHS = new Set(KEEP_ALIVE_DEFS.map(d => d.path))

// ── KeepAlivePage ─────────────────────────────────────────────────────────────

function KeepAlivePage({ path, component: Component }: { path: string; component: ComponentType }) {
  const { kept, keepRoute } = useTabs()
  const location = useLocation()
  const isActive = location.pathname === path || location.pathname.startsWith(path + '/')

  useEffect(() => {
    if (isActive) keepRoute(path)
  }, [isActive, path, keepRoute])

  if (!kept.has(path) && !isActive) return null

  return (
    <div className={isActive ? 'flex-1 min-h-0 overflow-y-auto' : 'hidden'}>
      <Component />
    </div>
  )
}

// ── Instrument keep-alive tabs ────────────────────────────────────────────────

// Registers the instrument tab on navigation (renders nothing itself)
function InstrumentRoute() {
  const { id = localStorage.getItem('last_instrument') || 'EURUSD=X' } = useParams<{ id: string }>()
  const { openInstrument } = useTabs()
  useEffect(() => { openInstrument(id.toUpperCase()) }, [id, openInstrument])
  return null
}

// Keeps one InstrumentDashboard mounted per open instrument tab
function InstrumentKeepAlive({ id }: { id: string }) {
  const location = useLocation()
  // location.pathname keeps its percent-encoding (EURUSD=X -> /instruments/EURUSD%3DX),
  // while `id` is already decoded (react-router's useParams decodes dynamic segments) —
  // comparing them as raw strings never matches for any id containing a character
  // encodeURIComponent escapes ("=" in EURUSD=X/USDJPY=X/GBPUSD=X in particular), so the
  // tab silently never activates and the page renders blank. Decode the URL segment
  // before comparing.
  let currentId = location.pathname.replace(/^\/instruments\//, '')
  try { currentId = decodeURIComponent(currentId) } catch { /* malformed sequence, compare as-is */ }
  const isActive = currentId.toLowerCase() === id.toLowerCase()
  return (
    <div className={isActive ? 'flex-1 min-h-0 overflow-y-auto' : 'hidden'}>
      <InstrumentDashboard instrumentIdProp={id} isVisible={isActive} />
    </div>
  )
}

// ── NormalRoutes — legacy redirects + instrument tab registration ─────────────

function NormalRoutes() {
  const location = useLocation()
  if (KEEP_ALIVE_PATHS.has(location.pathname)) return null

  // On instrument paths the keep-alive layer renders the UI; this Routes just registers the tab.
  // Was `/^\/instruments\/\w/` (required a word char right after the slash) — encoded ids
  // that start with a character encodeURIComponent escapes (^GSPC -> %5EGSPC) begin with
  // "%", which isn't \w, so this evaluated false for that tab. isInstrument then picked
  // 'flex-1 overflow-auto' instead of 'hidden' for this otherwise-empty div, and — since
  // InstrumentKeepAlive's own div is ALSO flex-1 in the same flex column — the two split
  // the available height roughly 50/50 instead of the instrument page getting all of it,
  // which is exactly the "page only fills half the screen" symptom.
  const isInstrument = location.pathname.startsWith('/instruments/')

  return (
    <div className={isInstrument ? 'hidden' : 'flex-1 overflow-auto'}>
      <Routes>
        <Route path="/instruments" element={<Navigate to={`/instruments/${localStorage.getItem('last_instrument') || 'EURUSD=X'}`} replace />} />
        <Route path="/instruments/:id" element={<InstrumentRoute />} />
        {/* Legacy redirects */}
        <Route path="/impact"    element={<Navigate to="/market-events" replace />} />
        <Route path="/timeline"  element={<Navigate to="/market-events" replace />} />
      </Routes>
    </div>
  )
}

// ── GlobalWatcher ─────────────────────────────────────────────────────────────

function GlobalWatcher() {
  useCycleWatcher()
  return null
}

// ── App ───────────────────────────────────────────────────────────────────────

function AppInner() {
  const { instrumentIds } = useTabs()
  return (
    <div className="flex h-screen overflow-hidden">
      <GlobalWatcher />
      <ChatWidget />
      <Sidebar />
      <div className="flex-1 flex flex-col min-h-0 overflow-hidden">
        <TabBar />
        <div className="flex-1 flex flex-col min-h-0">
          {KEEP_ALIVE_DEFS.map(def => (
            <KeepAlivePage key={def.path} path={def.path} component={def.component} />
          ))}
          {instrumentIds.map(id => (
            <InstrumentKeepAlive key={id} id={id} />
          ))}
          <NormalRoutes />
        </div>
      </div>
    </div>
  )
}

export default function App() {
  return (
    <BrowserRouter>
      <TabsProvider>
        <AppInner />
      </TabsProvider>
    </BrowserRouter>
  )
}
