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 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 IconMingcuteBack2Line from '~icons/mingcute/back-2-line';
import IconMingcutePlayLine from '~icons/mingcute/play-line';

export type ProjectRequirementStatusActionCode =
  | 'claim_to_review'
  | 'claim_to_implement'
  | 'pass_review'
  | 'reject_review'
  | 'start_implement'
  | 'accept'
  | 'reject'
  | 'cancel'
  | 'close';

export const projectRequirementStatusRecord: Record<Api.Project.ProjectRequirementStatusCode, string> = {
  pending_claim: '待认领',
  pending_review: '待评审',
  reviewed: '已评审',
  review_rejected: '评审未过',
  implementing: '实施中',
  accepted: '已验收',
  closed: '已关闭',
  rejected: '已拒绝',
  cancelled: '已取消'
};

transformRecordToOption(projectRequirementStatusRecord);

export const projectRequirementStatusActionRecord: Record<ProjectRequirementStatusActionCode, string> = {
  claim_to_review: '认领',
  claim_to_implement: '认领',
  pass_review: '评审通过',
  reject_review: '评审不通过',
  start_implement: '开始实施',
  accept: '验收通过',
  reject: '拒绝',
  cancel: '取消',
  close: '关闭'
};

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

export const DEFAULT_ACTION_ICON = markRaw(IconMdiSync);

function resolveActionKeyword(actionCode: string) {
  return Object.keys(ACTION_ICON_MAP).find(keyword => actionCode.includes(keyword));
}

/**
 * 判断动作是否为终态动作
 *
 * 终态动作定义：执行后需求进入终态（closed/rejected/cancelled）
 * 包括：reject, cancel, close
 */
export function isProjectRequirementActionTerminal(actionCode: string) {
  const terminalActions = new Set(['reject', 'cancel', 'close']);
  return terminalActions.has(actionCode);
}

/**
 * 获取操作按钮的颜色类型
 *
 * 成功/审批类 → success，危险操作 → danger，其他 → primary
 */
export function getProjectRequirementActionButtonType(actionCode: string): 'primary' | 'success' | 'danger' {
  if (['reject', 'cancel', 'close', 'delete'].includes(actionCode)) {
    return 'danger';
  }
  // if (['accept', 'pass_review'].includes(actionCode)) {
  //   return 'success';
  // }
  return 'primary';
}

/**
 * 获取操作动作的展示名称
 */
export function getProjectRequirementActionDisplayName(action: Api.Project.ProjectRequirementLifecycleAction) {
  if (action.actionCode === 'claim_to_review' || action.actionCode === 'claim_to_implement') {
    return '认领';
  }

  return action.actionName || action.actionCode;
}

/**
 * 根据 actionCode 获取对应图标
 */
export function getProjectRequirementActionIcon(actionCode: string) {
  const actionKeyword = resolveActionKeyword(actionCode);
  return actionKeyword ? ACTION_ICON_MAP[actionKeyword] : DEFAULT_ACTION_ICON;
}
