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

// Auth
import { LoginPage } from '@/pages/auth/Login'
import { ForgotPasswordPage } from '@/pages/auth/ForgotPassword'
import { RegisterPage } from '@/pages/auth/Register'

// Marketplace
import { CartPage } from '@/pages/marketplace/Cart'
import { CheckoutPage } from '@/pages/marketplace/Checkout'
import { OrdersPage as UserOrdersPage } from '@/pages/marketplace/Orders'
import { OrderDetailsPage } from '@/pages/marketplace/OrderDetails'
import ProductSearch from '@/pages/marketplace/ProductSearch'

// Seller dashboard
import { SellerDashboardPage } from '@/pages/dashboard/seller/SellerDashboard'
import { InventoryPage } from '@/pages/dashboard/seller/Inventory'
import { ProductCreatePage } from '@/pages/dashboard/seller/ProductCreate'
import { WalletPage } from '@/pages/dashboard/seller/Wallet'
import { TeamPage } from '@/pages/dashboard/seller/Team'

// Other role dashboards
import { EmployeeDashboardPage } from '@/pages/dashboard/employee/EmployeeDashboard'
import { DeliveryDashboardPage } from '@/pages/dashboard/delivery/DeliveryDashboard'

// Shared dashboard pages
import { CompanyPage } from '@/pages/dashboard/Company'
import { MyProfilePage } from '@/pages/dashboard/MyProfile'

// Admin dashboard
import {
  DashboardHome,
  UsersPage,
  CompaniesPage,
  ProductsPage,
  OrdersPage,
  ReviewsPage,
  LogisticsPage,
  ProfilePage,
  ShippingSettingsPage,
} from '@/pages/dashboard/admin'

// Shared UI
import { ProtectedRoute } from '@/components/ProtectedRoute'
import { DashboardLayout } from '@/layouts/DashboardLayout'

function App() {
  return (
    <Routes>
      <Route path="/login" element={<LoginPage />} />
      <Route path="/forgot-password" element={<ForgotPasswordPage />} />
      <Route path="/register" element={<RegisterPage />} />

      {/* Admin Dashboard with Header Layout */}
      <Route
        path="/dashboard"
        element={
          <ProtectedRoute allowedRoles={['admin']}>
            <DashboardLayout />
          </ProtectedRoute>
        }
      >
        <Route index element={<DashboardHome />} />
        <Route path="users" element={<UsersPage />} />
        <Route path="companies" element={<CompaniesPage />} />
        <Route path="products" element={<ProductsPage />} />
        <Route path="orders" element={<OrdersPage />} />
        <Route path="reviews" element={<ReviewsPage />} />
        <Route path="logistics" element={<LogisticsPage />} />
        <Route path="profile" element={<ProfilePage />} />
      </Route>

      {/* Legacy admin route - redirect to dashboard */}
      <Route path="/admin" element={<Navigate to="/dashboard" replace />} />

      {/* Owner / Seller Dashboard */}
      <Route
        path="/seller"
        element={
          <ProtectedRoute allowedRoles={['owner', 'seller']}>
            <SellerDashboardPage />
          </ProtectedRoute>
        }
      />

      {/* Employee (Colaborador) Dashboard */}
      <Route
        path="/colaborador"
        element={
          <ProtectedRoute allowedRoles={['employee']}>
            <EmployeeDashboardPage />
          </ProtectedRoute>
        }
      />

      {/* Delivery (Entregador) Dashboard */}
      <Route
        path="/entregas"
        element={
          <ProtectedRoute allowedRoles={['delivery']}>
            <DeliveryDashboardPage />
          </ProtectedRoute>
        }
      />

      <Route
        path="/cart"
        element={
          <ProtectedRoute allowedRoles={['owner', 'seller', 'employee']}>
            <CartPage />
          </ProtectedRoute>
        }
      />
      <Route
        path="/orders"
        element={
          <ProtectedRoute allowedRoles={['owner', 'seller', 'employee']}>
            <UserOrdersPage />
          </ProtectedRoute>
        }
      />
      <Route
        path="/inventory"
        element={
          <ProtectedRoute allowedRoles={['owner', 'seller']}>
            <InventoryPage />
          </ProtectedRoute>
        }
      />
      <Route
        path="/products/new"
        element={
          <ProtectedRoute allowedRoles={['owner', 'seller']}>
            <ProductCreatePage />
          </ProtectedRoute>
        }
      />
      <Route
        path="/company"
        element={
          <ProtectedRoute>
            <CompanyPage />
          </ProtectedRoute>
        }
      />
      <Route
        path="/seller-dashboard"
        element={
          <ProtectedRoute>
            <SellerDashboardPage />
          </ProtectedRoute>
        }
      />
      <Route
        path="/profile"
        element={
          <ProtectedRoute allowedRoles={['admin']}>
            <ProfilePage />
          </ProtectedRoute>
        }
      />
      <Route
        path="/meu-perfil"
        element={
          <ProtectedRoute allowedRoles={['owner', 'seller']}>
            <MyProfilePage />
          </ProtectedRoute>
        }
      />
      <Route
        path="/wallet"
        element={
          <ProtectedRoute allowedRoles={['owner', 'seller']}>
            <WalletPage />
          </ProtectedRoute>
        }
      />
      <Route
        path="/checkout"
        element={
          <ProtectedRoute allowedRoles={['owner', 'seller', 'employee']}>
            <CheckoutPage />
          </ProtectedRoute>
        }
      />
      {/* Product Search - Buy from other pharmacies */}
      <Route
        path="/search"
        element={
          <ProtectedRoute allowedRoles={['owner', 'seller', 'employee']}>
            <ProductSearch />
          </ProtectedRoute>
        }
      />
      {/* Shipping Settings - for Sellers */}
      <Route
        path="/shipping-settings"
        element={
          <ProtectedRoute allowedRoles={['owner', 'seller']}>
            <ShippingSettingsPage />
          </ProtectedRoute>
        }
      />
      {/* Team Management */}
      <Route
        path="/team"
        element={
          <ProtectedRoute allowedRoles={['owner', 'seller']}>
            <TeamPage />
          </ProtectedRoute>
        }
      />
      {/* Order Details Timeline */}
      <Route
        path="/orders/:id"
        element={
          <ProtectedRoute>
            <OrderDetailsPage />
          </ProtectedRoute>
        }
      />
      <Route path="*" element={<Navigate to="/login" replace />} />
    </Routes>
  )
}

export default App
