import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom'
import { useAuthStore } from './store/authStore'
import AppLayout from './components/layout/AppLayout'

// Auth
import LoginPage from './pages/auth/LoginPage'

// Pages
import DashboardPage      from './pages/dashboard/DashboardPage'
import ClientsPage        from './pages/clients/ClientsPage'
import ClientDetailPage   from './pages/clients/ClientDetailPage'
import ClientFormPage     from './pages/clients/ClientFormPage'
import LockersPage        from './pages/lockers/LockersPage'
import LockerDetailPage   from './pages/lockers/LockerDetailPage'
import RentalsPage        from './pages/rentals/RentalsPage'
import RentalDetailPage   from './pages/rentals/RentalDetailPage'
import NewRentalPage      from './pages/rentals/NewRentalPage'
import PaymentsPage       from './pages/payments/PaymentsPage'
import ReportsPage        from './pages/reports/ReportsPage'
import SettingsPage       from './pages/settings/SettingsPage'
import UsersPage          from './pages/users/UsersPage'

function PrivateRoute({ children }: { children: React.ReactNode }) {
  const token = useAuthStore((s) => s.token)
  return token ? <>{children}</> : <Navigate to="/login" replace />
}

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

        <Route
          path="/"
          element={
            <PrivateRoute>
              <AppLayout />
            </PrivateRoute>
          }
        >
          <Route index element={<Navigate to="/dashboard" replace />} />
          <Route path="dashboard"          element={<DashboardPage />} />

          <Route path="clients"            element={<ClientsPage />} />
          <Route path="clients/new"        element={<ClientFormPage />} />
          <Route path="clients/:id"        element={<ClientDetailPage />} />
          <Route path="clients/:id/edit"   element={<ClientFormPage />} />

          <Route path="lockers"            element={<LockersPage />} />
          <Route path="lockers/:id"        element={<LockerDetailPage />} />

          <Route path="rentals"            element={<RentalsPage />} />
          <Route path="rentals/new"        element={<NewRentalPage />} />
          <Route path="rentals/:id"        element={<RentalDetailPage />} />

          <Route path="payments"           element={<PaymentsPage />} />
          <Route path="reports"            element={<ReportsPage />} />
          <Route path="settings"           element={<SettingsPage />} />
          <Route path="users"              element={<UsersPage />} />
        </Route>

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