import { Navigate, Route, Routes, useLocation } from 'react-router-dom'
import { useAuthStore } from '../context/authStore'

import AuthGuard from '../hooks/useAuthGuard'
import LoginPage from '../pages/LoginPage'
import RegisterPage from '../pages/RegisterPage'
import DashboardPage from '../pages/DashboardPage'
import ProductsPage from '../pages/ProductsPage'
import WarehousesPage from '../pages/WarehousesPage'
import TransactionsPage from '../pages/TransactionsPage'
import ReportsPage from '../pages/ReportsPage'
import AppLayout from '../layouts/AppLayout'

export default function AppRoutes() {
  const location = useLocation()
  const { isAuthenticated } = useAuthStore()

  return (
    <Routes location={location}>
      <Route path="/login" element={<LoginPage />} />
      <Route path="/register" element={<RegisterPage />} />

      <Route element={<AuthGuard />}>
        <Route element={<AppLayout />}>
          <Route path="/dashboard" element={<DashboardPage />} />
          <Route path="/products" element={<ProductsPage />} />
          <Route path="/warehouses" element={<WarehousesPage />} />
          <Route path="/transactions" element={<TransactionsPage />} />
          <Route path="/reports" element={<ReportsPage />} />
        </Route>
      </Route>

      <Route
        path="*"
        element={<Navigate to={isAuthenticated ? '/dashboard' : '/login'} replace />}
      />
    </Routes>
  )
}


