import { Navigate, Route, Routes } from "react-router-dom";

import { AppLayout } from "@/components/layout/AppLayout";
import { useAuthStore } from "@/store/authStore";
import { BookingDetailPage } from "@/pages/BookingDetailPage";
import { BookingsPage } from "@/pages/BookingsPage";
import { CalendarPage } from "@/pages/CalendarPage";
import { CustomersPage } from "@/pages/CustomersPage";
import { DashboardPage } from "@/pages/DashboardPage";
import { DiscountCodesPage } from "@/pages/DiscountCodesPage";
import { LoginPage } from "@/pages/LoginPage";
import { NewBookingPage } from "@/pages/NewBookingPage";
import { PaymentsPage } from "@/pages/PaymentsPage";
import { ReferralCodesPage } from "@/pages/ReferralCodesPage";
import { ReservationsPage } from "@/pages/ReservationsPage";
import { RoomsPage } from "@/pages/RoomsPage";
import { SettingsPage } from "@/pages/SettingsPage";
import { TransactionsPage } from "@/pages/TransactionsPage";
import { VisitsPage } from "@/pages/VisitsPage";
import { ManageUsersPage } from "@/pages/ManageUsersPage";

function ProtectedLayout() {
  const accessToken = useAuthStore((s) => s.accessToken);
  const bootstrapped = useAuthStore((s) => s.bootstrapped);
  if (!bootstrapped) {
    return (
      <div className="flex min-h-screen items-center justify-center text-muted-foreground">
        Loading…
      </div>
    );
  }
  if (!accessToken) return <Navigate to="/login" replace />;
  return <AppLayout />;
}

export default function App() {
  return (
    <Routes>
      <Route path="/login" element={<LoginPage />} />
      <Route element={<ProtectedLayout />}>
        <Route path="/" element={<Navigate to="/dashboard" replace />} />
        <Route path="/dashboard" element={<DashboardPage />} />
        <Route path="/reservations" element={<ReservationsPage />} />
        <Route path="/bookings" element={<BookingsPage />} />
        <Route path="/bookings/new" element={<NewBookingPage />} />
        <Route path="/bookings/:id" element={<BookingDetailPage />} />
        <Route path="/calendar" element={<CalendarPage />} />
        <Route path="/rooms" element={<RoomsPage />} />
        <Route path="/customers" element={<CustomersPage />} />
        <Route path="/transactions" element={<TransactionsPage />} />
        <Route path="/payments" element={<PaymentsPage />} />
        <Route path="/marketing/visits" element={<VisitsPage />} />
        <Route path="/marketing/discount-codes" element={<DiscountCodesPage />} />
        <Route path="/marketing/referral-codes" element={<ReferralCodesPage />} />
        <Route path="/settings/users" element={<ManageUsersPage />} />
        <Route path="/settings" element={<SettingsPage />} />
      </Route>
      <Route path="*" element={<Navigate to="/dashboard" replace />} />
    </Routes>
  );
}
