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 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';
import IconMingcuteForward2Line from '~icons/mingcute/forward-2-line';

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

export const requirementStatusRecord: Record<Api.Product.RequirementStatusCode, string> = {
  pending_claim: '待认领',
  pending_review: '待评审',
  pending_dispatch: '待指派',
  reviewed: '已评审',
  review_rejected: '评审未过',
  implementing: '实施中',
  accepted: '已验收',
  closed: '已关闭',
  rejected: '已拒绝',
  cancelled: '已取消'
};
transformRecordToOption(requirementStatusRecord);

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

/**
 * 操作按钮图标映射
 *
 * 将操作类型映射到对应的 Iconify 图标组件
 */
export const ACTION_ICON_MAP: Record<string, object> = {
  split: markRaw(IconTablerSitemap),
  edit: markRaw(IconMdiPencilOutline),
  forward: markRaw(IconMingcuteForward2Line),
  claim_to_review: markRaw(IconMaterialSymbolsDescriptionOutline),
  claim_to_dispatch: markRaw(IconMdiCheckOutline),
  pass_review: markRaw(IconMdiGlasses),
  reject_review: 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',
  pass_review: 'primary',
  reject_review: 'danger',
  dispatch: 'primary',
  accept: 'primary',
  reject: 'danger',
  cancel: 'danger',
  close: 'danger',
  delete: 'danger'
};

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;
}
