import { createBrowserRouter, Navigate } from 'react-router';
import type { ReactNode } from 'react';
import { AppLayout } from './layout/AppLayout';
import { DashboardPage } from './pages/DashboardPage';
import { LoginPage } from './pages/LoginPage';
import { BusinessPage } from './pages/BusinessPage';
import { QuestionBankPage } from './pages/QuestionBankPage';
import { CommercialWorkbenchPage } from './pages/CommercialWorkbenchPage';
import { RequirePermission } from './auth/RequirePermission';
import { ForbiddenPage } from './pages/ForbiddenPage';
import { ApprovalCenterPage } from './pages/ApprovalCenterPage';
import { ConfigurationCenterPage, NotificationCenterPage } from './pages/GovernanceCenterPage';

const protectedPage = (permissions: readonly string[], element: ReactNode) =>
  <RequirePermission anyOf={permissions}>{element}</RequirePermission>;

export const router = createBrowserRouter([
  { path: '/login', element: <LoginPage /> },
  {
    path: '/',
    element: <AppLayout />,
    children: [
      { index: true, element: protectedPage(['platform:dashboard:view'], <DashboardPage />) },
      { path: 'tenants', element: protectedPage(['platform:tenant:manage'], <CommercialWorkbenchPage workbenchKey="onboarding" />) },
      { path: 'subscriptions', element: protectedPage(['platform:saas-catalog:manage', 'platform:saas-billing:manage'], <CommercialWorkbenchPage workbenchKey="receivables" />) },
      { path: 'billing', element: <Navigate to="/subscriptions" replace /> },
      { path: 'usage', element: protectedPage(['platform:saas-billing:manage'], <BusinessPage pageKey="usage" />) },
      { path: 'dunning', element: protectedPage(['platform:billing:notification', 'platform:saas-billing:manage'], <CommercialWorkbenchPage workbenchKey="dunning" />) },
      { path: 'refunds', element: protectedPage(['platform:saas-billing:manage'], <CommercialWorkbenchPage workbenchKey="refunds" />) },
      { path: 'question-banks', element: protectedPage(['platform:question-bank:manage'], <QuestionBankPage />) },
      { path: 'crm', element: protectedPage(['platform:crm:read'], <BusinessPage pageKey="crm" />) },
      { path: 'sms', element: protectedPage(['platform:sms:read'], <BusinessPage pageKey="sms" />) },
      { path: 'payments', element: protectedPage(['platform:payment:read'], <BusinessPage pageKey="payments" />) },
      { path: 'staff', element: protectedPage(['platform:staff:manage', 'platform:role:manage'], <BusinessPage pageKey="staff" />) },
      { path: 'audit', element: protectedPage(['platform:audit:view'], <BusinessPage pageKey="audit" />) },
      { path: 'alerts', element: protectedPage(['platform:audit:view'], <BusinessPage pageKey="alerts" />) },
      { path: 'operations', element: protectedPage(['platform:operations:view'], <BusinessPage pageKey="operations" />) },
      { path: 'approvals', element: protectedPage(['platform:approval:view'], <ApprovalCenterPage />) },
      { path: 'configuration', element: protectedPage(['platform:configuration:manage'], <ConfigurationCenterPage />) },
      { path: 'notifications', element: protectedPage(['platform:notification:manage'], <NotificationCenterPage />) },
      { path: 'forbidden', element: <ForbiddenPage /> },
    ],
  },
  { path: '*', element: <Navigate to="/" replace /> },
]);
