import {
  createBrowserRouter,
  RouterProvider,
  Navigate,
} from 'react-router-dom';
import {
  LoginPage,
  ForgotPasswordPage,
  SetPasswordPage,
  OTPVerificationPage,
} from '@ema-platform/auth';
import { AuthLayout } from '../layouts/AuthLayout';
import { BackofficeLayout } from '../layouts/BackofficeLayout';
import { ProtectedRoute } from './ProtectedRoute';
import { DashboardPage } from '../features/dashboard/pages/DashboardPage';
import UserManagementPage from '../features/user-management/UserManagementPage';
import { ProfilePage } from '../features/profile/pages/ProfilePage';
import { ConfigurationPage } from '../features/configuration/pages/ConfigurationPage';
import { LocationPage } from '../features/location/pages/LocationPage';
import { AnalyticsPage } from '../features/analytics/pages/AnalyticsPage';
import { ApplicationReviewPage } from '../features/applications/pages/ApplicationReviewPage';
import { MedicalVerificationPage } from '../features/medical-verification/pages/MedicalVerificationPage';
import { PaymentConfigPage } from '../features/payment-config/pages/PaymentConfigPage';
import { SeafarerRegistryPage } from '../features/seafarer-registry/pages/SeafarerRegistryPage';
import { SeamanBookQueuePage } from '../features/seaman-book-queue/pages/SeamanBookQueuePage';
import { QuestionPage } from '../features/question/pages/QuestionPage';
import { ExamPage } from '../features/exam/pages/ExamPage';
import { ExamDetailPage } from '../features/exam/pages/ExamDetailPage';
import { ResultPage } from '../features/result/pages/ResultPage';
import { ExamAppealsPage } from '../features/result/pages/ExamAppealsPage';
import { VesselRegistrationQueuePage } from '../features/vessel-registration/pages/VesselRegistrationQueuePage';
import { VesselRegistrationReviewPage } from '../features/vessel-registration/pages/VesselRegistrationReviewPage';
import { VesselRegistrationReportPage } from '../features/vessel-registration/pages/VesselRegistrationReportPage';
import { VesselRegistrationFormBuilderPage } from '../features/vessel-registration/pages/VesselRegistrationFormBuilderPage';
import { VesselRegistrationHeadDashboardPage } from '../features/vessel-registration-head/pages/VesselRegistrationHeadDashboardPage';
import { LicenseQueuePage } from '../features/license-review/pages/LicenseQueuePage';
import { LicenseRegisterPage } from '../features/license-register/pages/LicenseRegisterPage';
import { LicenseReviewPage } from '../features/license-review/pages/LicenseReviewPage';
import { LogisticsHeadDashboardPage } from '../features/logistics-head/pages/LogisticsHeadDashboardPage';
import { CertificateDesignerPage } from '../features/certificate-designer/pages/CertificateDesignerPage';

const router = createBrowserRouter([
  {
    element: <AuthLayout />,
    children: [
      { path: '/login', element: <LoginPage /> },
      { path: '/forgot-password', element: <ForgotPasswordPage /> },
      { path: '/set-password', element: <SetPasswordPage /> },
      { path: '/reset-password', element: <SetPasswordPage /> },
      { path: '/otp-verify', element: <OTPVerificationPage /> },
    ],
  },
  { path: '/um/*', element: <UserManagementPage /> },
  { path: '/', element: <Navigate to="/dashboard" replace /> },
  { path: '/profile-setup', element: <Navigate to="/dashboard" replace /> },
  {
    element: <ProtectedRoute />,
    children: [
      {
        element: <BackofficeLayout />,
        children: [
          { index: true, element: <Navigate to="/dashboard" replace /> },
          { path: 'dashboard', element: <DashboardPage /> },
          { path: 'vessel-registration-head-dashboard', element: <VesselRegistrationHeadDashboardPage /> },
          { path: 'logistics-head-dashboard', element: <LogisticsHeadDashboardPage /> },
          { path: 'profile', element: <ProfilePage /> },
          { path: 'configuration', element: <ConfigurationPage /> },
          { path: 'locations', element: <LocationPage /> },
          { path: 'analytics', element: <AnalyticsPage /> },
          { path: 'applications/:id', element: <ApplicationReviewPage /> },
          // CoC/CoP review happens in the config-driven licence queue.
          { path: 'coc-queue', element: <Navigate to="/licence-review/type/CERTIFICATE_OF_COMPETENCY" replace /> },
          { path: 'coc-queue/:id', element: <Navigate to="/licence-review" replace /> },
          // Endorsement review happens in the config-driven licence queue.
          { path: 'endorsement-queue', element: <Navigate to="/licence-review/type/ENDORSEMENT_COC" replace /> },
          { path: 'endorsement-queue/:id', element: <Navigate to="/licence-review" replace /> },
          { path: 'medical-verification', element: <MedicalVerificationPage /> },
          { path: 'payment-config', element: <PaymentConfigPage /> },
          { path: 'seafarer-registry', element: <SeafarerRegistryPage /> },
          { path: 'seaman-book-queue', element: <SeamanBookQueuePage /> },
          { path: 'questions', element: <QuestionPage /> },
          { path: 'exams', element: <ExamPage /> },
          { path: 'exams/:id', element: <ExamDetailPage /> },
          { path: 'exam-results', element: <ResultPage /> },
          { path: 'exam-appeals', element: <ExamAppealsPage /> },
          { path: 'vessel-registration-queue', element: <VesselRegistrationQueuePage /> },
          { path: 'vessel-registration-queue/new', element: <VesselRegistrationFormBuilderPage /> },
         // { path: 'vessel-registration-queue/:id', element: <VesselRegistrationReviewPage /> },
          //{ path: 'vessel-registration-report', element: <VesselRegistrationReportPage /> },
          { path: 'vessel-ownership-transfer', element: <Navigate to="/licence-review/type/VESSEL_OWNERSHIP_TRANSFER" replace /> },
          { path: 'vessel-ownership-transfer/:id', element: <Navigate to="/licence-review" replace /> },
          // Config-driven review workspace, shared by every licence type.
          { path: 'certificate-designer', element: <CertificateDesignerPage /> },
          { path: 'licence-review', element: <LicenseQueuePage /> },
          { path: 'licence-register', element: <LicenseRegisterPage /> },
          // Deep link into the grid with the type facet pinned, so "Freight
          // Forwarder" in the nav is a filtered view rather than a page.
          { path: 'licence-review/type/:typeCode', element: <LicenseQueuePage /> },
          { path: 'licence-review/:id', element: <LicenseReviewPage /> },
          { path: 'freight-forwarder-license', element: <Navigate to="/licence-review" replace /> },
          { path: 'freight-forwarder-license/:id', element: <Navigate to="/licence-review" replace /> },
          { path: 'shipping-agent-license', element: <Navigate to="/licence-review" replace /> },
          { path: 'shipping-agent-license/:id', element: <Navigate to="/licence-review" replace /> },
          { path: 'combined-license', element: <Navigate to="/licence-review" replace /> },
          { path: 'combined-license/:id', element: <Navigate to="/licence-review" replace /> },
          { path: 'joint-investment-license', element: <Navigate to="/licence-review" replace /> },
          { path: 'joint-investment-license/:id', element: <Navigate to="/licence-review" replace /> },
          { path: 'mto-license', element: <Navigate to="/licence-review" replace /> },
          { path: 'mto-license/:id', element: <Navigate to="/licence-review" replace /> },
          // Waivers are seeded licence types, so their queue is the licence
          // queue filtered by type and their review is the licence review.
          { path: 'waiver', element: <Navigate to="/licence-review/type/PRE_WAIVER" replace /> },
          { path: 'waiver/:id', element: <Navigate to="/licence-review" replace /> },
        ],
      },
    ],
  },
  { path: '/404', element: <div>Page not found</div> },
  { path: '*', element: <Navigate to="/404" replace /> },
]);

export function AppRouter() {
  return <RouterProvider router={router} />;
}
