import { Alert, AlertProps } from '@tidbcloud/uikit'
import { IconAlertTriangle } from '@tidbcloud/uikit/icons'

import Link from 'dbaas/components/Link'

import type { StarterInventoryLimitStatus } from './use-starter-inventory-limit'

const INSTANCE_CAPACITY_PLAN_URL = 'https://www.pingcap.com/programs/agentic-ai-instance-capacity'

interface StarterCapacityAlertProps extends Omit<AlertProps, 'children'> {
  status: StarterInventoryLimitStatus
  currentCount?: number
  limit?: number
}

export const StarterCapacityAlert = ({ status, currentCount, limit, ...props }: StarterCapacityAlertProps) => {
  const isLimitReached = status === 'reached'
  const usageText =
    typeof currentCount === 'number' && typeof limit === 'number' ? ` Current usage: ${currentCount}` : ''

  return (
    <Alert
      color={isLimitReached ? 'red' : 'yellow'}
      icon={<IconAlertTriangle size={14} />}
      data-testid="starter-capacity-alert"
      {...props}
    >
      {isLimitReached ? (
        <>
          Your organization has reached the instance capacity limit of {limit} free resources (include all instances and
          branches). You cannot create new resources at this time. Need to create more?{' '}
          <Link href={INSTANCE_CAPACITY_PLAN_URL} target="_blank" rel="noreferrer" noTrack>
            Apply for Instance Capacity Extension
          </Link>
          .
        </>
      ) : (
        <>
          Your organization is nearing the instance capacity limit of {limit} free resources.{usageText} (include all
          instances and branches).{' '}
          <Link href={INSTANCE_CAPACITY_PLAN_URL} target="_blank" rel="noreferrer" noTrack>
            Apply for Instance Capacity Extension
          </Link>
          .
        </>
      )}
    </Alert>
  )
}
