import { Tag, Tooltip } from 'antd'
import type { TagProps } from 'antd'
import type { DefaultOptionType } from 'antd/es/select'
import React from 'react'

const STATE_TAG_MAP: Record<string, { color: TagProps['color'] }> = {
  active: {
    color: 'success'
  },
  inactive: {
    color: 'default'
  }
}

export const renderRegionState = (state?: string) => {
  if (!state) {
    return '-'
  }

  const normalizedState = state.toLowerCase()
  const tagConfig = STATE_TAG_MAP[normalizedState] || {
    color: 'default' as TagProps['color'],
    label: state
  }

  return <Tag color={tagConfig.color}>{state}</Tag>
}

export const DEPLOY_TYPE_OPTIONS = [
  { label: 'Starter & Essential', value: 'DEPLOY_TYPE_SERVERLESS' },
  { label: 'Dedicated', value: 'DEPLOY_TYPE_DEDICATED' },
  {
    label: (
      <Tooltip title="Premium, BYOC, Essential_V2, Premium_Reserved">
        <span>Premium & BYOC & Essential_V2 & Premium_Reserved</span>
      </Tooltip>
    ),
    value: 'DEPLOY_TYPE_NEXTGEN'
  }
]

export const REGION_STATE_OPTIONS: DefaultOptionType[] = [
  { label: 'ACTIVE', value: 'ACTIVE' },
  { label: 'INACTIVE', value: 'INACTIVE' }
]

export const SERVICE_PLAN_TAG_COLORS: Record<string, string> = {
  Starter: '#A21182',
  Essential: '#7B37BD',
  Essential_V2: '#5E2794',
  BYOC: '#0F46B8',
  Premium_Reserved: '#3A98CD',
  Premium: '#117BBF',
  Dedicated: '#2299A1'
}

export const SERVICE_PLAN_BORDER_COLORS: Record<string, string> = {
  ...SERVICE_PLAN_TAG_COLORS
}

export const renderServicePlanTag = (servicePlan?: string) => {
  if (!servicePlan) {
    return '-'
  }

  const color = SERVICE_PLAN_TAG_COLORS[servicePlan] || '#8c8c8c'
  return (
    <Tag
      style={{
        color,
        borderColor: color,
        backgroundColor: `${color}1A`
      }}
    >
      {servicePlan}
    </Tag>
  )
}

const CLOUD_PROVIDER_OPTIONS_MAP: Partial<Record<string, DefaultOptionType[]>> =
  {
    DEPLOY_TYPE_SERVERLESS: [
      { label: 'AWS', value: 'AWS' },
      { label: 'ALICLOUD', value: 'ALICLOUD' }
    ]
  }

const SERVICE_PLAN_OPTIONS_MAP: Partial<Record<string, DefaultOptionType[]>> = {
  DEPLOY_TYPE_SERVERLESS: [
    { label: 'Starter', value: 'Starter' },
    { label: 'Essential', value: 'Essential' }
  ]
}

export function getCloudProviderOptions(
  deployType: string
): DefaultOptionType[] {
  return CLOUD_PROVIDER_OPTIONS_MAP[deployType] ?? []
}

export function getServicePlanOptions(deployType: string): DefaultOptionType[] {
  return SERVICE_PLAN_OPTIONS_MAP[deployType] ?? []
}
