import { lazy } from 'react'
import { RouteProps } from 'react-router'
import { Redirect } from 'react-router-dom'

import type { V1PrincipalProfileRoles } from 'apiClient/services/core-server/interface'
import CCComponent from 'pages/ConfigCenter/Component'
import VersionReleaseChange from 'pages/ConfigCenter/DefaultConfigChange'
import EnvironmentReleaseDetail from 'pages/ConfigCenter/DefaultConfigChange/EnvironmentReleaseDetail'
import ConfigTemplates from 'pages/ConfigCenter/Template'
import TemplateDetail from 'pages/ConfigCenter/Template/TemplateDetail'
import VersionReleaseList from 'pages/ConfigCenter/VersionRelease'
import CPPODetails from 'pages/CPPO/Details'
import MCPODetails from 'pages/MCPO/Details'
import RegionDetail from 'pages/Region/RegionDetail'
import RegionList from 'pages/Region/RegionList'
import { SKUList } from 'pages/SKU'
import { SKUService } from 'pages/SKUService'
import ClusterConfigHistoryDetail from 'pages/Tenants/ClusterDetail/Configs/Histories/History'
import ChangefeedDetail from 'pages/Tenants/ClusterDetail/TiCDC/ChangefeedDetail'
import TiCDCClusterDetail from 'pages/Tenants/ClusterDetail/TiCDC/TiCDCDetail'
import ToolsPage from 'pages/Tenants/ClusterDetail/TiDBTools'
import ReplayDetail from 'pages/Tenants/ClusterDetail/TrafficMirror/ReplayDetail'

import {
  Audits,
  DBaaSBRAC,
  Overview,
  Providers,
  Profile,
  SettingsPanel,
  Incidents,
  MSP,
  CPPO,
  MCPO,
  Resale
} from './pages'

const MSPDetails = lazy(() => import('pages/MSP/Details'))

const ConfigCenter = lazy(() => import('pages/ConfigCenter'))
const Tenants = lazy(() => import('./pages/Tenants'))
const JumpAuthorizations = lazy(
  () => import('pages/Devops/JumpServerAuthorization')
)
const JumpAuthorizationDetail = lazy(
  () => import('pages/Devops/JumpServerAuthorization/AuthorizationDetail')
)
const Maintenance = lazy(() => import('pages/Devops/Maintenance'))
const MaintenanceDetail = lazy(() => import('pages/Devops/Maintenance/Detail'))
const DevOpsOperations = lazy(() => import('pages/Devops/Operations'))
const TenantCreditsMonthlyReport = lazy(
  () => import('pages/Devops/Operations/TenantCreditsMonthlyReport')
)
const OperationDetail = lazy(
  () => import('pages/Devops/Operations/OperationDetail')
)
const ChangeDetailPage = lazy(
  () => import('pages/Devops/Release/Change/ChangeDetail')
)

const Release = lazy(() => import('./pages/Devops/Release'))
const ReleaseDetail = lazy(
  () => import('pages/Devops/Release/Release/ReleaseDetail')
)
const TicketDetail = lazy(
  () => import('pages/Devops/Release/Ticket/TicketDetail')
)
const UpdateChangePage = lazy(
  () => import('./pages/Devops/Release/Change/UpdateChangePage')
)

const IncidentDetail = lazy(() => import('./pages/Incidents/IncidentView'))

const Certification = lazy(() => import('./pages/Monitoring/Certification'))
const ComponentDiff = lazy(() => import('./pages/Monitoring/ComponentDiff'))
const InfraInfo = lazy(() => import('./pages/Monitoring/InfraInformation'))
const InfraInfoDetail = lazy(
  () => import('./pages/Monitoring/InfraInformation/InfraInfoDetail')
)
const K8sUpgradeTaskList = lazy(
  () => import('./pages/Devops/K8sUpgrade/TaskList')
)
const K8sUpgradeTaskDetail = lazy(
  () => import('./pages/Devops/K8sUpgrade/TaskDetail')
)
const K8sUpgradeTaskK8sClusterDetail = lazy(
  () => import('./pages/Devops/K8sUpgrade/K8sClusterDetail')
)
const K8sUpgradeClusterSummary = lazy(
  () => import('./pages/Devops/K8sUpgrade/ClusterSummary')
)
const Deployments = lazy(() => import('pages/Devops/Deployment'))
const DeploymentDetail = lazy(
  () => import('pages/Devops/Deployment/DeploymentDetail')
)
const SharedResourcePool = lazy(() => import('pages/Devops/SharedResourcePool'))
const K8sCalendar = lazy(() => import('pages/Devops/K8sCalendar'))
const K8sUpgradePlan = lazy(() => import('pages/Devops/K8sUpgradePlan'))
const KernelImages = lazy(() => import('pages/Devops/KernelImages'))
const PremiumUpgradePlan = lazy(() => import('pages/Devops/PremiumUpgradePlan'))
const PremiumUpgradePlanCreate = lazy(
  () => import('pages/Devops/PremiumUpgradePlan/CreatePlan')
)
const PremiumUpgradePlanDetail = lazy(
  () => import('pages/Devops/PremiumUpgradePlan/PlanDetail')
)
const ClusterMigration = lazy(() => import('pages/Devops/ClusterMigration'))
const ClusterMigrationPrecheckDetail = lazy(
  () => import('pages/Devops/ClusterMigration/PrecheckDetail')
)

const ProviderDetail = lazy(() => import('./pages/Providers/Detail'))
const ClusterDetail = lazy(() => import('./pages/Tenants/ClusterDetail'))
const ProjectDetail = lazy(() => import('./pages/Tenants/ProjectDetail'))
const ByocClusterDetail = lazy(
  () => import('./pages/Tenants/ClusterDetail/ByocClusterDetail')
)
const Terminal = lazy(
  () => import('./pages/Tenants/ClusterDetail/Maintance/terminal')
)
const TenantDetail = lazy(() => import('./pages/Tenants/TenantDetail'))

const NotificationDetail = lazy(
  () => import('./pages/Notification/NotificationDetail')
)

const NotificationStatistics = lazy(
  () => import('./pages/Notification/NotificationStatistics')
)

const Notification = lazy(() => import('./pages/Notification'))

const UserBehaviorSubscriptions = lazy(
  () => import('./pages/UserBehavior/Subscriptions')
)

const SLAMonthlySummaryPage = lazy(() => import('./pages/Overview/SLA/Summary'))

const SLAClusterDetailPage = lazy(() => import('./pages/Overview/SLA/Detail'))

const SLAMonthlySummaryOfNextGenPage = lazy(
  () => import('./pages/Overview/SLA/SummaryNextGen')
)

const SLAClusterDetailOfNextGenPage = lazy(
  () => import('./pages/Overview/SLA/DetailNextGen')
)

const RemediationTickets = lazy(() => import('./pages/RemediationTickets'))

const RemediationTicketDetail = lazy(
  () => import('./pages/RemediationTickets/RemediationTicketDetail')
)

const IAMEntity = lazy(() => import('./pages/IAM/Entity'))
const IAMUserDetail = lazy(() => import('./pages/IAM/Entity/User/UserDetail'))
const IAMUserGroupDetail = lazy(
  () => import('./pages/IAM/Entity/UserGroup/UserGroupDetail')
)
const IAMAPIKeyDetail = lazy(
  () => import('./pages/IAM/Entity/APIKey/APIKeyDetail')
)
const IAMRole = lazy(() => import('./pages/IAM/Role'))
const IAMPolicyDetail = lazy(
  () => import('./pages/IAM/Role/Policy/PolicyDetail')
)
const IAMRoleDetail = lazy(() => import('./pages/IAM/Role/Role/RoleDetail'))

const Privilege = lazy(() => import('./pages/Admin/Privilege'))
const PrivilegeDetail = lazy(
  () => import('./pages/Admin/Privilege/PrivilegeDetail')
)
const Calendar = lazy(() => import('./pages/Admin/Calendar'))
const Agents = lazy(() => import('./pages/Admin/Agents'))
const Playground = lazy(() => import('./pages/Playground'))

const ResaleDetails = lazy(() => import('./pages/Resale/Details'))

const JiraIssues = lazy(() => import('pages/AIOps/Issues'))
const JiraIssueDetail = lazy(() => import('pages/AIOps/IssueDetail'))

const JiraAlerts = lazy(() => import('pages/AIOps/Alerts'))
const JiraAlertDetail = lazy(() => import('pages/AIOps/AlertDetail'))

const ReportDetail = lazy(() => import('pages/AIOps/ReportDetail'))

const FeatureWhitelist = lazy(() => import('./pages/FeatureWhitelist'))

const allRoutes: RouteProps[] = [
  {
    component: Overview,
    path: '/overview',
    exact: true
  },
  {
    component: Profile,
    path: '/profile',
    exact: true
  },
  {
    component: Tenants,
    path: '/orgs',
    exact: true
  },
  {
    component: TenantDetail,
    path: '/orgs/:org_id',
    exact: true
  },
  {
    component: ClusterConfigHistoryDetail,
    path: '/confighistories/:history_id',
    exact: true
  },
  {
    component: ClusterDetail,
    path: '/orgs/:org_id/clusters/:cluster_id',
    exact: true
  },
  {
    component: ProjectDetail,
    path: '/orgs/:org_id/projects/:project_id',
    exact: true
  },
  {
    component: ByocClusterDetail,
    path: '/orgs/:org_id/byoc-clusters/:cluster_id',
    exact: true
  },
  {
    exact: true,
    path: '/operations/add-credits-monthly-report',
    render: ({ location }) => (
      <Redirect
        to={{
          ...location,
          pathname: '/operations/tenant-credits-monthly-report'
        }}
      />
    )
  },
  {
    component: TenantCreditsMonthlyReport,
    path: '/operations/tenant-credits-monthly-report',
    exact: true
  },
  {
    component: OperationDetail,
    path: '/operations/:instance_id',
    exact: true
  },
  {
    component: DevOpsOperations,
    path: '/operations'
  },
  {
    component: DeploymentDetail,
    path: '/deployments/:deployment_id',
    exact: true
  },
  {
    component: Deployments,
    path: '/deployments',
    exact: true
  },
  {
    component: KernelImages,
    path: '/kernel-images',
    exact: true
  },
  {
    component: JumpAuthorizations,
    path: '/jumps',
    exact: true
  },
  {
    component: JumpAuthorizationDetail,
    path: '/jumps/:authorization_id'
  },
  {
    component: Terminal,
    path: '/orgs/:org_id/clusters/:cluster_id/ctl'
  },
  {
    component: Providers,
    path: '/providers',
    exact: true
  },
  {
    component: ProviderDetail,
    path: '/providers/:provider'
  },
  {
    component: UpdateChangePage,
    path: '/release/:release_id/:change_id/update',
    exact: true
  },
  {
    component: ChangeDetailPage,
    path: '/release/:release_id/change/:change_id',
    exact: true
  },
  {
    component: TicketDetail,
    path: '/release/:release_id/tickets/:name'
  },
  {
    component: ReleaseDetail,
    path: '/release/:release_id'
  },
  {
    component: Release,
    path: '/release'
  },
  {
    component: DBaaSBRAC,
    path: '/rbac'
  },
  {
    component: Incidents,
    path: '/incidents',
    exact: true
  },
  {
    component: IncidentDetail,
    path: '/incidents/:incident_id'
  },
  {
    component: Terminal,
    path: '/clusters/:cluster_id/ctl'
  },
  {
    component: MaintenanceDetail,
    path: '/maintenance/:id'
  },
  {
    component: Maintenance,
    path: '/maintenance'
  },
  {
    component: Certification,
    path: '/certification'
  },
  {
    component: ComponentDiff,
    path: '/component-diff'
  },
  {
    component: InfraInfo,
    path: '/infraInformation',
    exact: true
  },
  {
    component: InfraInfoDetail,
    path: '/infraInformation/:k8s_name',
    exact: true
  },
  {
    component: K8sUpgradeTaskList,
    path: '/k8sUpgradeTask',
    exact: true
  },
  {
    component: K8sUpgradeClusterSummary,
    path: '/k8sUpgradeTask/clusterSummary',
    exact: true
  },
  {
    component: K8sUpgradeTaskK8sClusterDetail,
    path: '/k8sUpgradeTask/:task_id(\\d+)/k8sCluster/:k8s_name',
    exact: true
  },
  {
    component: K8sUpgradeTaskDetail,
    path: '/k8sUpgradeTask/:task_id(\\d+)',
    exact: true
  },
  {
    component: K8sCalendar,
    path: '/k8scalendar',
    exact: true
  },
  {
    component: K8sUpgradePlan,
    path: '/k8supgradeplan',
    exact: true
  },
  {
    component: PremiumUpgradePlan,
    path: '/premiumUpgradePlans',
    exact: true
  },
  {
    component: PremiumUpgradePlanCreate,
    path: '/premiumUpgradePlans/new',
    exact: true
  },
  {
    component: PremiumUpgradePlanDetail,
    path: '/premiumUpgradePlans/:planId(\\d+)',
    exact: true
  },
  {
    component: PremiumUpgradePlan,
    path: '/premiumUpgradePlan',
    exact: true
  },
  {
    component: ClusterMigrationPrecheckDetail,
    path: '/cluster-migration/precheck/:id',
    exact: true
  },
  {
    component: ClusterMigration,
    path: '/cluster-migration',
    exact: true
  },
  {
    component: SharedResourcePool,
    path: '/sharedResourcePool',
    exact: true
  },
  {
    component: SharedResourcePool,
    path: '/sharedResourcePool/:resource_pool_id',
    exact: true
  },
  {
    component: SettingsPanel,
    path: '/business'
  },
  {
    component: MSPDetails,
    path: '/msp/:org_id'
  },
  {
    component: MSP,
    path: '/msp'
  },
  {
    component: CPPODetails,
    path: '/cppo/:org_id'
  },
  {
    component: CPPO,
    path: '/cppo'
  },
  {
    component: MCPODetails,
    path: '/mcpo/:org_id'
  },
  {
    component: MCPO,
    path: '/mcpo'
  },
  {
    component: ConfigCenter,
    path: '/dict'
  },
  {
    component: TemplateDetail,
    path: '/template/:template_id',
    exact: true
  },
  {
    component: ConfigTemplates,
    path: '/template',
    exact: true
  },
  {
    component: CCComponent,
    path: '/component',
    exact: true
  },
  {
    component: VersionReleaseChange,
    path: '/defaultconfig/change/:change_id',
    exact: true
  },
  {
    component: EnvironmentReleaseDetail,
    path: '/defaultconfig/change/:change_id/release/:release_id',
    exact: true
  },
  {
    component: Notification,
    path: '/notification',
    exact: true
  },
  {
    component: NotificationDetail,
    path: '/notification/detail/:workflow_id?',
    exact: true
  },
  {
    component: NotificationStatistics,
    path: '/notification/:workflow_id/statistics',
    exact: true
  },
  {
    component: UserBehaviorSubscriptions,
    path: '/userbehavior_subscriptions',
    exact: true
  },
  {
    component: VersionReleaseList,
    path: '/version'
  },
  {
    component: SLAMonthlySummaryPage,
    path: '/sla/summary'
  },
  {
    component: SLAClusterDetailPage,
    path: '/sla/:cluster_id/detail'
  },
  {
    component: SLAMonthlySummaryOfNextGenPage,
    path: '/sla/summary_next_gen'
  },
  {
    component: SLAClusterDetailOfNextGenPage,
    path: '/sla/:cluster_id/detail_next_gen'
  },
  {
    component: RemediationTickets,
    path: '/remediation_tickets',
    exact: true
  },
  {
    component: RemediationTicketDetail,
    path: '/remediation_tickets/:ticket_id',
    exact: true
  },
  {
    component: SKUList,
    path: '/sku_editor',
    exact: true
  },
  {
    component: SKUService,
    path: '/sku_service/:sku_component'
  },
  {
    component: Playground,
    path: '/playground'
  },
  {
    component: ChangefeedDetail,
    path: '/orgs/:org_id/clusters/:cluster_id/changefeeds/:changefeed_id',
    exact: true
  },
  {
    component: ToolsPage,
    path: '/orgs/:org_id/clusters/:cluster_id/tidbtools',
    exact: true
  },
  {
    component: TiCDCClusterDetail,
    path: '/orgs/:org_id/clusters/:cluster_id/ticdc_clusters/:ticdc_cluster_id',
    exact: true
  },
  {
    component: ReplayDetail,
    path: '/orgs/:org_id/clusters/:cluster_id/replays/:replay_id',
    exact: true
  },
  {
    component: ResaleDetails,
    path: '/reseller/:org_id'
  },
  {
    component: Resale,
    path: '/reseller'
  },
  {
    component: JiraIssues,
    path: '/aiops/issues',
    exact: true
  },
  {
    component: JiraIssueDetail,
    path: '/aiops/issues/:issue_key',
    exact: true
  },
  {
    component: JiraAlerts,
    path: '/aiops/alerts',
    exact: true
  },
  {
    component: JiraAlertDetail,
    path: '/aiops/alerts/:issue_key',
    exact: true
  },
  {
    component: ReportDetail,
    path: '/aiops/reports/:report_id',
    exact: true
  },
  {
    component: RegionList,
    path: '/regions',
    exact: true
  },
  {
    component: RegionDetail,
    path: '/regions/:logical_name',
    exact: true
  },
  {
    component: FeatureWhitelist,
    path: '/feature_whitelist',
    exact: true
  }
]

const adminRoute: RouteProps[] = [
  {
    component: Agents,
    path: '/agents'
  },
  {
    component: Calendar,
    path: '/calendar'
  },
  {
    component: Audits,
    path: '/audits'
  },
  {
    component: PrivilegeDetail,
    path: '/privilege/:priv_id'
  },
  {
    component: Privilege,
    path: '/privilege'
  }
]

const iamRoute: RouteProps[] = [
  {
    component: IAMAPIKeyDetail,
    path: '/iam/entity/apikeys/:key_name'
  },
  {
    component: IAMUserGroupDetail,
    path: '/iam/entity/usergroups/:user_group'
  },
  {
    component: IAMUserDetail,
    path: '/iam/entity/users/:user_email'
  },
  {
    component: IAMEntity,
    path: '/iam/entity'
  },
  {
    component: IAMPolicyDetail,
    path: '/iam/role/policies/:policy_name'
  },
  {
    component: IAMRoleDetail,
    path: '/iam/role/roles/:role_name'
  },
  {
    component: IAMRole,
    path: '/iam/role'
  }
]

export function getAllowedRoutes(roles?: V1PrincipalProfileRoles) {
  if (roles?.admin) {
    return [...allRoutes, ...adminRoute, ...iamRoute]
  } else {
    return allRoutes
  }
}

export default allRoutes
