import { createBrowserRouter } from 'react-router-dom';
import { App } from './App';
import { DashboardLayout } from './components/layout/DashboardLayout';
import { RequireAuth } from './components/layout/RequireAuth';
import { HomePage } from './pages/HomePage';
import { LoginPage } from './pages/LoginPage';
import { SignUpPage } from './pages/SignUpPage';
import { SellItemPage } from './pages/SellItemPage';
import { ProductDetailPage } from './pages/ProductDetailPage';
import { CreateProfilePage } from './pages/CreateProfilePage';
import { UpdateProfilePage } from './pages/UpdateProfilePage';
import { ChatPage } from './pages/ChatPage';
import { MyOffersPage } from './pages/MyOffersPage';
import { NotificationsPage } from './pages/NotificationsPage';
import { SoldItemsPage } from './pages/SoldItemsPage';
import { SettingsPage } from './pages/SettingsPage';
import { MyListingsPage } from './pages/MyListingsPage';
import { SavedItemsPage } from './pages/SavedItemsPage';
import { AboutPage, PrivacyPage, TermsPage, HelpPage, ContactPage, ReturnsPage } from './pages/StaticPages';
import { RequireRole } from './components/layout/RequireRole';
import { AdminLayout } from './components/layout/AdminLayout';
import { AdminDashboardPage } from './pages/admin/AdminDashboardPage';
import { AdminUsersPage } from './pages/admin/AdminUsersPage';
import { AdminUserDetailPage } from './pages/admin/AdminUserDetailPage';
import { AdminListingsPage } from './pages/admin/AdminListingsPage';
import { AdminReportsPage } from './pages/admin/AdminReportsPage';
import { AdminModerationPage } from './pages/admin/AdminModerationPage';
import { AdminPaymentsPage } from './pages/admin/AdminPaymentsPage';
import { AdminSettingsPage } from './pages/admin/AdminSettingsPage';
// Rental imports
import { RentalsPage } from './pages/RentalsPage';
import { RentalDetailPage } from './pages/RentalDetailPage';
import { CreateRentalPage } from './pages/CreateRentalPage';
import { EditRentalPage } from './pages/EditRentalPage';
import { MyBookingsPage } from './pages/MyBookingsPage';
import { LandlordLayout } from './components/layout/LandlordLayout';
import { LandlordDashboardPage } from './pages/landlord/LandlordDashboardPage';
import { LandlordListingsPage } from './pages/landlord/LandlordListingsPage';
import { LandlordBookingsPage } from './pages/landlord/LandlordBookingsPage';
import { LandlordCalendarPage } from './pages/landlord/LandlordCalendarPage';
import { LandlordPayoutsPage } from './pages/landlord/LandlordPayoutsPage';
import { LandlordReviewsPage } from './pages/landlord/LandlordReviewsPage';
import { AdminRentalsPage } from './pages/admin/AdminRentalsPage';
import { AdminBookingsPage } from './pages/admin/AdminBookingsPage';
import { AdminRentalPayoutsPage } from './pages/admin/AdminRentalPayoutsPage';

export const router = createBrowserRouter([
  {
    path: '/',
    element: <App />,
    children: [
      { index: true, element: <HomePage /> },
      { path: 'login', element: <LoginPage /> },
      { path: 'signup', element: <SignUpPage /> },
      { path: 'sell', element: <RequireAuth><SellItemPage /></RequireAuth> },
      { path: 'listings/:id', element: <ProductDetailPage /> },
      { path: 'profile/create', element: <RequireAuth><CreateProfilePage /></RequireAuth> },
      { path: 'profile/edit', element: <RequireAuth><UpdateProfilePage /></RequireAuth> },
      { path: 'about', element: <AboutPage /> },
      { path: 'privacy', element: <PrivacyPage /> },
      { path: 'terms', element: <TermsPage /> },
      { path: 'help', element: <HelpPage /> },
      { path: 'contact', element: <ContactPage /> },
      { path: 'returns', element: <ReturnsPage /> },
      // Rental routes
      { path: 'rentals', element: <RentalsPage /> },
      { path: 'rentals/new', element: <RequireAuth><CreateRentalPage /></RequireAuth> },
      { path: 'rentals/:id', element: <RentalDetailPage /> },
      { path: 'rentals/:id/edit', element: <RequireAuth><EditRentalPage /></RequireAuth> },
      {
        path: 'admin',
        element: <RequireRole roles={['MODERATOR', 'ADMIN', 'SUPER_ADMIN']}><AdminLayout /></RequireRole>,
        children: [
          { index: true, element: <AdminDashboardPage /> },
          { path: 'users', element: <AdminUsersPage /> },
          { path: 'users/:id', element: <AdminUserDetailPage /> },
          { path: 'listings', element: <AdminListingsPage /> },
          { path: 'reports', element: <AdminReportsPage /> },
          { path: 'moderation', element: <AdminModerationPage /> },
          { path: 'payments', element: <AdminPaymentsPage /> },
          { path: 'settings', element: <AdminSettingsPage /> },
          { path: 'rentals', element: <AdminRentalsPage /> },
          { path: 'bookings', element: <AdminBookingsPage /> },
          { path: 'rental-payouts', element: <AdminRentalPayoutsPage /> },
        ],
      },
      {
        path: 'dashboard',
        element: <RequireAuth><DashboardLayout /></RequireAuth>,
        children: [
          { path: 'messages', element: <ChatPage /> },
          { path: 'offers', element: <MyOffersPage /> },
          { path: 'notifications', element: <NotificationsPage /> },
          { path: 'sold', element: <SoldItemsPage /> },
          { path: 'settings', element: <SettingsPage /> },
          { path: 'listings', element: <MyListingsPage /> },
          { path: 'saved', element: <SavedItemsPage /> },
          { path: 'bookings', element: <MyBookingsPage /> },
          { path: 'saved-rentals', element: <SavedItemsPage /> },
        ],
      },
      {
        path: 'landlord',
        element: <RequireAuth><LandlordLayout /></RequireAuth>,
        children: [
          { index: true, element: <LandlordDashboardPage /> },
          { path: 'listings', element: <LandlordListingsPage /> },
          { path: 'bookings', element: <LandlordBookingsPage /> },
          { path: 'calendar', element: <LandlordCalendarPage /> },
          { path: 'payouts', element: <LandlordPayoutsPage /> },
          { path: 'reviews', element: <LandlordReviewsPage /> },
        ],
      },
    ],
  },
]);
