import { Navigate, Route, Routes } from 'react-router-dom'
import { LoginPage } from './pages/Login'
import { CartPage } from './pages/Cart'
import { OrdersPage as UserOrdersPage } from './pages/Orders'
import { InventoryPage } from './pages/Inventory'
import { ProductCreatePage } from './pages/ProductCreate'
import { CompanyPage } from './pages/Company'
import { SellerDashboardPage } from './pages/SellerDashboard'
import { EmployeeDashboardPage } from './pages/EmployeeDashboard'
import { DeliveryDashboardPage } from './pages/DeliveryDashboard'
import { MyProfilePage } from './pages/MyProfile'
import { CheckoutPage } from './pages/Checkout'
import ProductSearch from './pages/ProductSearch'
import { ProtectedRoute } from './components/ProtectedRoute'
import { DashboardLayout } from './layouts/DashboardLayout'
import {
  DashboardHome,
  UsersPage,
  CompaniesPage,
  ProductsPage,
  OrdersPage,
  ReviewsPage,
  LogisticsPage,
  ProfilePage,
  ShippingSettingsPage
} from './pages/admin'

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

      {/* 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>
            <CartPage />
          </ProtectedRoute>
        }
      />
      <Route
        path="/orders"
        element={
          <ProtectedRoute>
            <UserOrdersPage />
          </ProtectedRoute>
        }
      />
      <Route
        path="/inventory"
        element={
          <ProtectedRoute>
            <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="/checkout"
        element={
          <ProtectedRoute>
            <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>
        }
      />
      <Route path="*" element={<Navigate to="/login" replace />} />
    </Routes>
  )
}

export default App
