import { Anchor, Typography } from '@tidbcloud/uikit'
import { IconLinkExternal01 } from '@tidbcloud/uikit/icons'
import { useMemo } from 'react'

import { type HoverHintContent, type HoverHintReason } from 'dbaas/components/HoverHint/ReasonHoverHint'

import { type BasicAccessCheckProps } from '../core/access-check-shared'
import { useAccessCheck } from '../core/use-access-check'

interface UseAccessDeniedHintReasonOptions extends BasicAccessCheckProps {
  /**
   * Hint content when access is denied.
   *
   * - If `undefined`, a default hint content will be used.
   * - If `null`, there will be no hint even when access is denied.
   */
  content?: HoverHintContent
  /**
   * Customize hover card display (e.g., id, positioning, styling).
   *
   * Note: `when` and `content` are auto-managed by this hook and the `content` option.
   */
  reason?: Partial<Omit<HoverHintReason, 'when' | 'content'>>
}

export const DEFAULT_HINT_CONTENT = (
  <Typography variant="body-lg">
    You do not have the required permissions.{' '}
    <Anchor
      href="https://docs.pingcap.com/tidbcloud/manage-user-access/"
      target="_blank"
      rel="noreferrer"
      sx={{ display: 'inline-flex', alignItems: 'center', gap: 4 }}
    >
      Learn more <IconLinkExternal01 />
    </Anchor>
  </Typography>
)

/**
 * Hook to create a hover hint reason for access denied scenarios.
 *
 * This hook combines access check with reason generation for use with `ReasonHoverHint`.
 * Use this when you need to compose multiple reasons (access + other conditions).
 *
 * For simple cases, prefer `AccessDeniedHoverHint` component instead.
 *
 * @example
 * // Combine access check with cluster state check
 * const { reason: accessDeniedReason } = useAccessDeniedHintReason({
 *   id: 'ClusterModifyGate'
 * })
 *
 * const clusterNotReadyReason: HoverHintReason = {
 *   id: 'clusterNotReady',
 *   when: !cluster.isReady,
 *   content: 'Cluster is not ready for modifications'
 * }
 *
 * <ReasonHoverHint reasons={[accessDeniedReason, clusterNotReadyReason]}>
 *   {({ matched }) => (
 *     <Button disabled={matched}>Edit Cluster</Button>
 *   )}
 * </ReasonHoverHint>
 *
 * @example
 * // Custom hint content
 * const { accessible, reason } = useAccessDeniedHintReason({
 *   id: 'OrgUsersAction',
 *   content: <Text>Only org admins can manage users</Text>
 * })
 *
 * @example
 * // No hint (only check access)
 * const { accessible } = useAccessDeniedHintReason({
 *   id: 'ProjectNetworkAction',
 *   content: null // No hover hint even when denied
 * })
 *
 * @returns Object containing `accessible` boolean and configured `reason` object
 */
export function useAccessDeniedHintReason({
  id,
  scopeProps,
  content = DEFAULT_HINT_CONTENT,
  reason: reasonOverrides
}: UseAccessDeniedHintReasonOptions) {
  const accessible = useAccessCheck(id, scopeProps)

  const reasonId = reasonOverrides?.id ?? 'accessDenied'
  const reason: HoverHintReason = useMemo(
    () => ({
      ...reasonOverrides,
      id: reasonId,
      when: !accessible,
      content
    }),
    [accessible, content, reasonId, reasonOverrides]
  )

  return {
    accessible,
    reason
  }
}
