import { markRaw } from 'vue';
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';

/**
 * 项目需求状态记录
 *
 * 来源：rdms_object_status_model 表 object_type='project_requirement' 的 7 条记录
 */
export const projectRequirementStatusRecord: Record<Api.Project.ProjectRequirementStatusCode, string> = {
  pending_confirm: '待确认',
  pending_review: '待评审',
  implementing: '实施中',
  accepted: '已验收',
  closed: '已关闭',
  rejected: '已拒绝',
  cancelled: '已取消'
};

/**
 * 终态状态码集合
 *
 * 来源：terminal_flag=1 的状态: closed, rejected, cancelled
 */
const TERMINAL_STATUS_SET = new Set<Api.Project.ProjectRequirementStatusCode>(['closed', 'rejected', 'cancelled']);

/**
 * 操作按钮图标映射
 *
 * 将操作类型映射到对应的 Iconify 图标组件
 */
export const ACTION_ICON_MAP: Record<string, object> = {
  split: markRaw(IconTablerSitemap),
  edit: markRaw(IconMdiPencilOutline),
  claim_to_review: markRaw(IconMdiCheckOutline),
  claim_to_implement: markRaw(IconMdiCheckCircleOutline),
  pass_review: markRaw(IconMdiGlasses),
  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));
}

/**
 * 获取项目需求状态的标签颜色
 */
export function getProjectRequirementStatusTagType(status: Api.Project.ProjectRequirementStatusCode): UI.ThemeColor {
  const statusTagTypeMap: Record<Api.Project.ProjectRequirementStatusCode, UI.ThemeColor> = {
    pending_confirm: 'info',
    pending_review: 'warning',
    implementing: 'primary',
    accepted: 'success',
    closed: 'info',
    rejected: 'danger',
    cancelled: 'danger'
  };

  return statusTagTypeMap[status];
}

/**
 * 判断状态是否为终态
 */
export function isProjectRequirementTerminal(statusCode: string) {
  return TERMINAL_STATUS_SET.has(statusCode as Api.Project.ProjectRequirementStatusCode);
}

/**
 * 判断动作是否为终态动作
 *
 * 终态动作定义：执行后需求进入终态（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) {
  return action.actionName || action.actionCode;
}

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