import { markRaw } from 'vue';
import { transformRecordToOption } from '@/utils/common';
import IconMdiPencilOutline from '~icons/mdi/pencil-outline';
import IconMdiCheckOutline from '~icons/mdi/check-outline';
import IconMdiCheckCircleOutline from '~icons/mdi/check-circle-outline';
import IconMdiSync from '~icons/mdi/sync';
import IconMdiPowerSettingsNew from '~icons/mdi/power-settings-new';
import IconMdiShareVariant from '~icons/mdi/share-variant';
import IconMdiDeleteOutline from '~icons/mdi/delete-outline';
import IconMdiGlasses from '~icons/mdi/glasses';
import IconMdiClose from '~icons/mdi/close';
import IconTablerSitemap from '~icons/tabler/sitemap';
import IconTablerCircleX from '~icons/tabler/circle-x';
import IconMaterialSymbolsDescriptionOutline from '~icons/material-symbols/description-outline';

export type RequirementStatusActionCode =
  | 'claim_to_review'
  | 'claim_to_dispatch'
  | 'reject'
  | 'to_dispatch'
  | 'dispatch'
  | 'cancel'
  | 'accept'
  | 'close';

export const requirementStatusRecord: Record<Api.Product.RequirementStatusCode, string> = {
  pending_confirm: '待确认',
  pending_review: '待评审',
  pending_dispatch: '待分流',
  implementing: '实施中',
  accepted: '已验收',
  closed: '已关闭',
  rejected: '已拒绝',
  cancelled: '已取消'
};

export const requirementStatusOptions = transformRecordToOption(requirementStatusRecord);

export const requirementStatusActionRecord: Record<RequirementStatusActionCode, string> = {
  claim_to_review: '认领',
  claim_to_dispatch: '认领',
  reject: '拒绝',
  to_dispatch: '评审通过',
  dispatch: '分流',
  cancel: '取消',
  accept: '验收通过',
  close: '关闭'
};

/**
 * 操作按钮图标映射
 *
 * 将操作类型映射到对应的 Iconify 图标组件
 */
export const ACTION_ICON_MAP: Record<string, object> = {
  split: markRaw(IconTablerSitemap),
  edit: markRaw(IconMdiPencilOutline),
  claim_to_review: markRaw(IconMaterialSymbolsDescriptionOutline),
  claim_to_dispatch: markRaw(IconMdiCheckOutline),
  to_dispatch: markRaw(IconMdiGlasses),
  dispatch: markRaw(IconMdiShareVariant),
  accept: markRaw(IconMdiCheckCircleOutline),
  reject: markRaw(IconMdiClose),
  cancel: markRaw(IconTablerCircleX),
  close: markRaw(IconMdiPowerSettingsNew),
  delete: markRaw(IconMdiDeleteOutline)
};

/**
 * 操作按钮颜色类型映射
 *
 * 审批/成功类操作 → success，危险操作 → danger，其他 → primary
 */
export const ACTION_TYPE_MAP: Record<string, 'primary' | 'success' | 'danger'> = {
  split: 'primary',
  edit: 'primary',
  claim_to_review: 'primary',
  claim_to_dispatch: 'primary',
  to_dispatch: 'primary',
  dispatch: 'primary',
  accept: 'primary',
  reject: 'danger',
  cancel: 'danger',
  close: 'danger',
  delete: 'danger'
};

export function getRequirementStatusLabel(status: Api.Product.RequirementStatusCode) {
  return requirementStatusRecord[status];
}

export function getRequirementStatusTagType(status: Api.Product.RequirementStatusCode): UI.ThemeColor {
  const statusTagTypeMap: Record<Api.Product.RequirementStatusCode, UI.ThemeColor> = {
    pending_confirm: 'info',
    pending_review: 'warning',
    pending_dispatch: 'primary',
    implementing: 'primary',
    accepted: 'success',
    closed: 'info',
    rejected: 'danger',
    cancelled: 'danger'
  };

  return statusTagTypeMap[status];
}

export function getRequirementActionLabel(actionCode: RequirementStatusActionCode) {
  return requirementStatusActionRecord[actionCode];
}

export function getRequirementActionTagType(
  actionCode: RequirementStatusActionCode
): 'primary' | 'success' | 'warning' | 'danger' | 'info' {
  const actionTagTypeMap: Record<RequirementStatusActionCode, 'primary' | 'success' | 'warning' | 'danger' | 'info'> = {
    claim_to_review: 'primary',
    claim_to_dispatch: 'primary',
    reject: 'danger',
    to_dispatch: 'success',
    dispatch: 'primary',
    cancel: 'danger',
    accept: 'success',
    close: 'info'
  };

  return actionTagTypeMap[actionCode];
}

export function isRequirementActionTerminal(actionCode: RequirementStatusActionCode) {
  const terminalActions: RequirementStatusActionCode[] = ['reject', 'cancel', 'close'];
  return terminalActions.includes(actionCode);
}

export function isRequirementActionNeedProject(actionCode: RequirementStatusActionCode) {
  return actionCode === 'dispatch';
}

export function isRequirementActionNeedReviewChoice(actionCode: RequirementStatusActionCode) {
  return actionCode === 'claim_to_review' || actionCode === 'claim_to_dispatch';
}

export function getRequirementActionDisplayName(action: Api.Product.RequirementLifecycleAction): string {
  const code = action.actionCode as RequirementStatusActionCode;

  if (code === 'claim_to_review' || code === 'claim_to_dispatch') {
    return '认领';
  }

  return action.actionName;
}
