import { SharedTiDB } from 'dbaas/screens/NextGen/shared/model/shared-tidb-state'
import { Commonv1beta1ClusterState } from 'dbaas/services/api-clients/v2023/cluster-management/v1beta1/interface'
import { useGetEnabledDPFeaturesForCluster } from 'dbaas/services/dataflow'

import { usePitrEnabled } from '../BackupRestore/helpers'
import { useHaveRunningChangefeed } from '../Changefeed/helpers'
import { SharedCluster } from '../ClusterManagement/cluster'
import { DataPlatformFeatureFlag } from '../Common'

export const IMPORT_IN_PROGRESS_TIP = `An import task is in progress. Please try again after it completes.`

export const getImportStatusWithConcurrentImport = (
  cluster: SharedCluster | null,
  allowConcurrentImport: boolean
): string => {
  if (cluster?.state === Commonv1beta1ClusterState.IMPORTING) {
    return allowConcurrentImport ? '' : IMPORT_IN_PROGRESS_TIP
  }

  return getImportStatusWithClusterStateCheckOnly(cluster)
}

/**
 * Import availability rules:
 *
 * Dedicated Cloud uses this hook and checks in order:
 * 1. IMPORTING
 *    - SupportParallelImport enabled: allow import.
 *    - SupportParallelImport disabled: block import.
 * 2. Cluster state
 *    - MAINTENANCE: block import.
 *    - ACTIVE: continue to Dedicated-only checks.
 *    - Other states: block import.
 * 3. Dedicated-only checks for ACTIVE clusters
 *    - Running CDC with SupportSkipCDCAndPITRCheckWhenImport enabled: continue.
 *    - Running CDC with SupportSkipCDCAndPITRCheckWhenImport disabled: block import.
 *    - PITR enabled with SupportSkipCDCAndPITRCheckWhenImport enabled: continue.
 *    - PITR enabled with SupportSkipCDCAndPITRCheckWhenImport disabled: block import.
 *
 * Premium and Serverless call getImportStatusWithClusterStateCheckOnly directly:
 * 1. MAINTENANCE: block import.
 * 2. ACTIVE: allow import.
 * 3. Other states: block import.
 */
export const useImportStatus = (orgId: string, projectId: string, cluster: SharedCluster | null) => {
  const { data: featureData } = useGetEnabledDPFeaturesForCluster(orgId, projectId, cluster?.id!, {
    query: {
      enabled: !!cluster && !!cluster.id && cluster?.computed?.isDedicated
    }
  })
  const allowConcurrentImport = featureData?.features?.includes(DataPlatformFeatureFlag.SupportParallelImport) ?? false
  const canSkipChangefeedPrecheck =
    featureData?.features?.includes(DataPlatformFeatureFlag.SupportSkipCDCAndPITRCheckWhenImport) ?? false

  const haveRunningChangefeed = useHaveRunningChangefeed(orgId, projectId, cluster)

  // check backup setting, if Point-in-Time Restore is enabled, it can't do import
  const pitrEnabled = usePitrEnabled(orgId, projectId, cluster)

  const importDisabledTips = (() => {
    if (cluster?.state === Commonv1beta1ClusterState.IMPORTING) {
      return getImportStatusWithConcurrentImport(cluster, allowConcurrentImport)
    }

    const stateTips = getImportStatusWithClusterStateCheckOnly(cluster)
    if (stateTips) {
      return stateTips
    }

    // early return for non-dedicated clusters
    if (!cluster?.computed?.isDedicated) {
      return ''
    }

    if (haveRunningChangefeed && !canSkipChangefeedPrecheck) {
      return `A changefeed is running. Stop it before creating an import task.`
    }

    if (pitrEnabled && !canSkipChangefeedPrecheck) {
      return `Point-in-Time Recovery is enabled. Disable PITR before creating an import task.`
    }

    return ''
  })()

  const disableImport = importDisabledTips.length > 0

  return {
    disableImport,
    importTips: importDisabledTips,
    showImport: true
  }
}

export const getImportStatusWithClusterStateCheckOnly = (cluster: SharedTiDB | SharedCluster | null): string => {
  if (!cluster) {
    return ''
  }

  if (cluster?.state === Commonv1beta1ClusterState.MAINTENANCE) {
    return `The cluster is under maintenance. Please retry once maintenance finishes.`
  }

  if (cluster?.state !== Commonv1beta1ClusterState.ACTIVE) {
    return `The cluster is not in an active state. Please try again later.`
  }

  return ''
}
