import { BrowserRouter, Navigate, Route, Routes } from 'react-router-dom'
import { Toaster } from 'sonner'
import { AuthProvider } from '@/contexts/AuthContext'
import { ProtectedRoute } from '@/components/ProtectedRoute'
import { Layout } from '@/components/Layout'
import { LoginPage } from '@/pages/LoginPage'
import { DashboardPage } from '@/pages/DashboardPage'
import { SuppliersPage } from '@/pages/master/SuppliersPage'
import { ProjectsPage } from '@/pages/master/ProjectsPage'
import { DepartmentsPage } from '@/pages/master/DepartmentsPage'
import { CatalogsPage } from '@/pages/master/CatalogsPage'
import { PermissionsPage } from '@/pages/system/PermissionsPage'
import { RolesPage } from '@/pages/system/RolesPage'
import { WorkflowsPage } from '@/pages/system/WorkflowsPage'
import { PeWorkflowsPage } from '@/pages/system/PeWorkflowsPage'
import { FormsPage } from '@/pages/forms/FormsPage'
import { ContractsListPage } from '@/pages/contracts/ContractsListPage'
import { ContractDetailPage } from '@/pages/contracts/ContractDetailPage'
import { ContractCreatePage } from '@/pages/contracts/ContractCreatePage'
import { ReportsPage } from '@/pages/ReportsPage'
import { UsersPage } from '@/pages/system/UsersPage'
import { PurchaseEvaluationsListPage, PurchaseEvaluationDetailPage } from '@/pages/pe/PurchaseEvaluationsListPage'
import { PurchaseEvaluationCreatePage } from '@/pages/pe/PurchaseEvaluationCreatePage'
import { PurchaseEvaluationWorkspacePage } from '@/pages/pe/PurchaseEvaluationWorkspacePage'
import { BudgetsListPage, BudgetDetailPage } from '@/pages/budgets/BudgetsListPage'
import { BudgetCreatePage } from '@/pages/budgets/BudgetCreatePage'

function App() {
  return (
    <BrowserRouter>
      <AuthProvider>
        <Routes>
          <Route path="/login" element={<LoginPage />} />
          <Route
            element={
              <ProtectedRoute>
                <Layout />
              </ProtectedRoute>
            }
          >
            <Route path="/dashboard" element={<DashboardPage />} />
            <Route path="/master/suppliers" element={<SuppliersPage />} />
            <Route path="/master/projects" element={<ProjectsPage />} />
            <Route path="/master/departments" element={<DepartmentsPage />} />
            <Route path="/master/catalogs" element={<Navigate to="/master/catalogs/units" replace />} />
            <Route path="/master/catalogs/:kind" element={<CatalogsPage />} />
            <Route path="/system/users" element={<UsersPage />} />
            <Route path="/system/roles" element={<RolesPage />} />
            <Route path="/system/permissions" element={<PermissionsPage />} />
            <Route path="/system/workflows" element={<WorkflowsPage />} />
            <Route path="/system/workflows/:typeCode" element={<WorkflowsPage />} />
            <Route path="/system/pe-workflows" element={<PeWorkflowsPage />} />
            <Route path="/system/pe-workflows/:typeCode" element={<PeWorkflowsPage />} />
            <Route path="/forms" element={<FormsPage />} />
            <Route path="/contracts" element={<ContractsListPage />} />
            <Route path="/contracts/new" element={<ContractCreatePage />} />
            <Route path="/contracts/:id" element={<ContractDetailPage />} />
            <Route path="/purchase-evaluations" element={<PurchaseEvaluationsListPage />} />
            <Route path="/purchase-evaluations/workspace" element={<PurchaseEvaluationWorkspacePage />} />
            <Route path="/purchase-evaluations/new" element={<PurchaseEvaluationCreatePage />} />
            <Route path="/purchase-evaluations/:id" element={<PurchaseEvaluationDetailPage />} />
            <Route path="/budgets" element={<BudgetsListPage />} />
            <Route path="/budgets/new" element={<BudgetCreatePage />} />
            <Route path="/budgets/:id" element={<BudgetDetailPage />} />
            <Route path="/reports" element={<ReportsPage />} />
            <Route path="/" element={<Navigate to="/dashboard" replace />} />
            <Route
              path="*"
              element={
                <div className="p-8 text-slate-500">
                  Trang này chưa được build — sẽ có ở Phase tiếp theo.
                </div>
              }
            />
          </Route>
        </Routes>
        <Toaster richColors position="top-right" />
      </AuthProvider>
    </BrowserRouter>
  )
}

export default App
