import { Navigate, Route, Routes } from 'react-router-dom'
import { LoginPage } from './pages/Login'
import { CartPage } from './pages/Cart'
import { CheckoutPage } from './pages/Checkout'
import { OrdersPage as UserOrdersPage } from './pages/Orders'
import { InventoryPage } from './pages/Inventory'
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 { ProtectedRoute } from './components/ProtectedRoute'
import { DashboardLayout } from './layouts/DashboardLayout'
import {
  DashboardHome,
  UsersPage,
  CompaniesPage,
  ProductsPage,
  OrdersPage,
  ReviewsPage,
  LogisticsPage,
  ProfilePage
} 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="/checkout"
        element={
          <ProtectedRoute>
            <CheckoutPage />
          </ProtectedRoute>
        }
      />
      <Route
        path="/orders"
        element={
          <ProtectedRoute>
            <UserOrdersPage />
          </ProtectedRoute>
        }
      />
      <Route
        path="/inventory"
        element={
          <ProtectedRoute>
            <InventoryPage />
          </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="/search" element={<Navigate to="/dashboard" replace />} />
      <Route path="*" element={<Navigate to="/login" replace />} />
    </Routes>
  )
}

export default App
