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 { UserDashboardPage } from '@/pages/UserDashboardPage'
import { ComingSoonPage } from '@/pages/ComingSoonPage'
import { InboxPage } from '@/pages/InboxPage'
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 { ContractCreatePage } from '@/pages/contracts/ContractCreatePage'
import { ContractDetailPage } from '@/pages/contracts/ContractDetailPage'
import { MyContractsPage } from '@/pages/contracts/MyContractsPage'
import { HardCopiesPage } from '@/pages/contracts/HardCopiesPage'
import { PurchaseEvaluationsListPage, PurchaseEvaluationDetailPage } from '@/pages/pe/PurchaseEvaluationsListPage'
import { PurchaseEvaluationCreatePage } from '@/pages/pe/PurchaseEvaluationCreatePage'
import { PurchaseEvaluationWorkspacePage } from '@/pages/pe/PurchaseEvaluationWorkspacePage'
import { WorkflowMatrixViewPage } from '@/pages/pe/WorkflowMatrixViewPage'
// [W2 KHKK S161] Kế hoạch ký kết HĐ (GĐ2) — 3 page mirror SHA256 với fe-admin.
import { KhkkListPage } from '@/pages/khkk/KhkkListPage'
import { KhkkCreatePage } from '@/pages/khkk/KhkkCreatePage'
import { KhkkDetailPage } from '@/pages/khkk/KhkkDetailPage'
import { EmployeesListPage } from '@/pages/hrm/EmployeesListPage'
import { EmployeeCreatePage } from '@/pages/hrm/EmployeeCreatePage'
import { HrmConfigsPage } from '@/pages/hrm/HrmConfigsPage'
import { InternalDirectoryPage } from '@/pages/office/InternalDirectoryPage'
import { MeetingCalendarPage } from '@/pages/office/MeetingCalendarPage'
import { MeetingRoomsPage } from '@/pages/office/MeetingRoomsPage'
import { ProposalCreatePage } from '@/pages/office/ProposalCreatePage'
import { ProposalDetailPage } from '@/pages/office/ProposalDetailPage'
import { ProposalsListPage } from '@/pages/office/ProposalsListPage'
import { WorkflowAppsListPage } from '@/pages/office/WorkflowAppsListPage'
import { WorkflowAppDetailPage } from '@/pages/office/WorkflowAppDetailPage'
import { ItTicketsPage } from '@/pages/office/ItTicketsPage'
import { MyAttendancePage } from '@/pages/office/MyAttendancePage'
import { OfficeDashboardPage } from '@/pages/office/OfficeDashboardPage'
import { HrmDashboardPage } from '@/pages/hrm/HrmDashboardPage'

function App() {
  return (
    <BrowserRouter>
      <AuthProvider>
        <Routes>
          <Route path="/login" element={<LoginPage />} />
          <Route
            element={
              <ProtectedRoute>
                <Layout />
              </ProtectedRoute>
            }
          >
            <Route path="/dashboard" element={<UserDashboardPage />} />
            {/* [S159] trang dich cho menu skeleton GD2/GD4 */}
            <Route path="/coming-soon" element={<ComingSoonPage />} />
            <Route path="/inbox" element={<InboxPage />} />
            <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="/contracts/new" element={<ContractCreatePage />} />
            <Route path="/contracts/:id" element={<ContractDetailPage />} />
            <Route path="/my-contracts" element={<MyContractsPage />} />
            {/* [W7 KHKK S161] GĐ4 "Hợp đồng cứng" — 7 leaf `Hdc_*` deep-link ?type=1..7
                (Layout.tsx staticMap). Không query = tất cả loại. */}
            <Route path="/hard-copies" element={<HardCopiesPage />} />
            <Route path="/purchase-evaluations" element={<PurchaseEvaluationsListPage />} />
            <Route path="/purchase-evaluations/workflow-matrix" element={<WorkflowMatrixViewPage />} />
            {/* [S159-đợt5] alias cho leaf "Luồng duyệt" của 7 nhóm HĐ (?type=3) */}
            <Route path="/contracts/workflow-matrix" element={<WorkflowMatrixViewPage />} />
            <Route path="/purchase-evaluations/workspace" element={<PurchaseEvaluationWorkspacePage />} />
            <Route path="/purchase-evaluations/new" element={<PurchaseEvaluationCreatePage />} />
            <Route path="/purchase-evaluations/:id" element={<PurchaseEvaluationDetailPage />} />
            {/* [W2 KHKK S161] Kế hoạch ký kết HĐ (GĐ2). `/khkk/workflow-matrix` = alias
                cho leaf "Luồng duyệt" (?type=10) — cùng khuôn alias `/contracts/workflow-matrix`
                của S159-đợt5, KHÔNG đẻ page mới. */}
            <Route path="/khkk" element={<Navigate to="/khkk/list" replace />} />
            <Route path="/khkk/list" element={<KhkkListPage />} />
            <Route path="/khkk/create" element={<KhkkCreatePage />} />
            <Route path="/khkk/workflow-matrix" element={<WorkflowMatrixViewPage />} />
            <Route path="/khkk/:id" element={<KhkkDetailPage />} />
            {/* Hồ sơ Nhân sự (Phase 10.1 G-H1 — Mig 34) */}
            <Route path="/employees" element={<EmployeesListPage />} />
            <Route path="/employees/new" element={<EmployeeCreatePage />} />
            {/* Cấu hình HRM (Phase 10.2 G-H2 — Mig 35) */}
            <Route path="/hrm/configs" element={<Navigate to="/hrm/configs/leave-types" replace />} />
            <Route path="/hrm/configs/:kind" element={<HrmConfigsPage />} />
            {/* Văn phòng số — Bảng điều khiển (landing dashboard, Off_Dashboard) */}
            <Route path="/office/dashboard" element={<OfficeDashboardPage />} />
            {/* Văn phòng số — Danh bạ nội bộ (Phase 10.2 G-O1) */}
            <Route path="/directory" element={<InternalDirectoryPage />} />
            {/* Văn phòng số — Phòng họp Booking + Catalog (Phase 10.2 G-O2 — Mig 36 S36) */}
            <Route path="/meeting-calendar" element={<MeetingCalendarPage />} />
            <Route path="/meeting-rooms" element={<MeetingRoomsPage />} />
            <Route path="/proposals" element={<ProposalsListPage />} />
            <Route path="/proposals/new" element={<ProposalCreatePage />} />
            <Route path="/proposals/:id" element={<ProposalDetailPage />} />
            <Route path="/workflow-apps/:kind" element={<WorkflowAppsListPage />} />
            <Route path="/workflow-apps/:kind/:id" element={<WorkflowAppDetailPage />} />
            <Route path="/it-tickets" element={<ItTicketsPage />} />
            <Route path="/attendance" element={<MyAttendancePage />} />
            <Route path="/hr/dashboard" element={<HrmDashboardPage />} />
            <Route path="/" element={<Navigate to="/dashboard" replace />} />
            <Route
              path="*"
              element={
                <div className="p-8 text-slate-500">
                  Trang này chưa được build.
                </div>
              }
            />
          </Route>
        </Routes>
        <Toaster richColors position="top-right" />
      </AuthProvider>
    </BrowserRouter>
  )
}

export default App
