import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom'
import { LoginPage } from './pages/LoginPage'
import { CpLayout } from './layouts/CpLayout'
import { Dashboard } from './pages/Dashboard'

function CpGuard({ children }: { children: React.ReactNode }) {
  const auth = localStorage.getItem('cpAuth')
  if (!auth) return <Navigate to="/login" replace />
  return <>{children}</>
}

export default function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/login" element={<LoginPage />} />

        <Route element={<CpGuard><CpLayout /></CpGuard>}>
          <Route path="/"             element={<Dashboard />} />
          <Route path="/hotels"       element={<Dashboard />} />
          <Route path="/cp-users"     element={<Dashboard />} />
          <Route path="/billing"      element={<Dashboard />} />
          <Route path="/monitoring"   element={<Dashboard />} />
          <Route path="/cp-settings"  element={<Dashboard />} />
        </Route>

        <Route path="*" element={<Navigate to="/" replace />} />
      </Routes>
    </BrowserRouter>
  )
}
