import { Anchor, Typography } from '@tidbcloud/uikit'

import { SharedCluster } from 'dbaas/features/ClusterManagement/cluster'
import { useRequestSupport } from 'dbaas/screens/HelpCenter/RequestSupport/helper'
import { compareVersion } from 'dbaas/utils/chore'

import { PRIVATE_LINK_STATUS } from './common'
import { useSqlEditorStatus } from './status'

interface SupportProps {
  cluster: SharedCluster | null
  subject?: string
  description?: string
}

export const Support: React.FC<React.PropsWithChildren<SupportProps>> = ({
  cluster,
  subject = '',
  description = ''
}) => {
  const { open } = useRequestSupport()
  return (
    <Anchor
      m="0 4px"
      onClick={() => {
        open({
          cluster: cluster || undefined,
          subject,
          description
        })
      }}
    >
      tech support
    </Anchor>
  )
}

export const useNavTooltip = (cluster: SharedCluster) => {
  const sqlEditorState = useSqlEditorStatus(cluster)

  if (!cluster) {
    return null
  }

  if (!sqlEditorState.disabled || sqlEditorState.isClusterStautsDisable) {
    return null
  }

  if (sqlEditorState.devTierDisabled) {
    return <Typography>Re-create a cluster to enter SQL Editor</Typography>
  }

  if (compareVersion(cluster.version, '6.5.0', '<')) {
    return (
      <Typography variant="body-lg">
        Upgrade your Dedicated cluster to TiDB 6.5 or above to access SQL Editor. Contact
        <Support
          cluster={cluster}
          subject="Upgrade my cluster to TiDB 6.5+ to access SQL Editor"
          description="Please upgrade my Dedicated cluster to TiDB 6.5+ so that I can access SQL Editor on it."
        />
        for assistance
      </Typography>
    )
  }

  if (
    [PRIVATE_LINK_STATUS.UNSUPPORT, PRIVATE_LINK_STATUS.NEED_CREATE].includes(
      sqlEditorState.privateLinkState || PRIVATE_LINK_STATUS.UNKNOWN
    )
  ) {
    return (
      <Typography variant="body-lg">
        Upgrade your cluster to access SQL Editor. Contact
        <Support
          cluster={cluster}
          subject="Upgrade my cluster to access SQL Editor"
          description="Please upgrade my Dedicated cluster so that I can access SQL Editor on it."
        />
        for assistance.
      </Typography>
    )
  }

  if ([PRIVATE_LINK_STATUS.CREATING].includes(sqlEditorState.privateLinkState || -1)) {
    // creating
    return (
      <Typography variant="body-lg">
        SQL Editor is preparing the environment and will be available in a few minutes. Please wait...
      </Typography>
    )
  }

  return null
}
