import { BrowserRouter, Routes, Route } from 'react-router-dom'
import Sidebar from './components/layout/Sidebar'
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 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 { Navigate } from 'react-router-dom'
import { useCycleWatcher } from './hooks/useApi'

function GlobalWatcher() {
  useCycleWatcher()
  return null
}

export default function App() {
  return (
    <BrowserRouter>
      <div className="flex min-h-screen">
        <GlobalWatcher />
        <Sidebar />
        <main className="flex-1 overflow-auto">
          <Routes>
            <Route path="/" element={<Dashboard />} />
            <Route path="/geo" element={<GeoRadar />} />
            <Route path="/markets" element={<Markets />} />
            <Route path="/macro" element={<MacroRegime />} />
            <Route path="/options" element={<OptionsLab />} />
            <Route path="/patterns" element={<PatternExplorer />} />
            <Route path="/patterns/edit" element={<PatternEditor />} />
            <Route path="/pattern-lab" element={<PatternLab />} />
            <Route path="/portfolio" element={<Portfolio />} />
            <Route path="/backtest" element={<Backtest />} />
            <Route path="/calendar" element={<CalendarPage />} />
            <Route path="/macro-series" element={<MacroSeriesPage />} />
            <Route path="/journal" element={<JournalDeBord />} />
            <Route path="/rapport" element={<RapportIA />} />
            <Route path="/super-contexte" element={<SuperContexte />} />
            <Route path="/config" element={<Config />} />
            <Route path="/analytics" element={<Analytics />} />
            <Route path="/analytics-advanced" element={<AnalyticsAdvanced />} />
            <Route path="/risk" element={<RiskDashboard />} />
            <Route path="/var" element={<VaRAnalysis />} />
            <Route path="/position-history" element={<PositionHistory />} />
            <Route path="/logs" element={<SystemLogs />} />
            <Route path="/institutional" element={<InstitutionalReports />} />
            <Route path="/specialist-desks" element={<SpecialistDesks />} />
            <Route path="/snapshot" element={<ExternalSnapshot />} />
            <Route path="/instruments" element={<Navigate to="/instruments/SPY" replace />} />
            <Route path="/instruments/:id" element={<InstrumentDashboard />} />
            {/* Legacy redirects */}
            <Route path="/impact" element={<Navigate to="/market-events" replace />} />
            <Route path="/timeline" element={<Navigate to="/market-events" replace />} />
            <Route path="/market-events" element={<MarketEvents />} />
            <Route path="/cycle-actions" element={<CycleActions />} />
            <Route path="/ai-desks" element={<AIDesks />} />
            <Route path="/simulator" element={<EuroSimulator />} />
            <Route path="/causal-lab" element={<CausalLab />} />
          </Routes>
        </main>
      </div>
    </BrowserRouter>
  )
}
