// 用户待办操作类型定义

/**
 * 操作状态
 */
export type PendingActionStatus = 'PENDING' | 'COMPLETED' | 'CANCELLED';

/**
 * 常用操作代码
 */
export const ACTION_CODES = {
  ADOPTION_WIZARD: 'ADOPTION_WIZARD', // 认种向导
  SETTLE_REWARDS: 'SETTLE_REWARDS', // 结算奖励
  BIND_PHONE: 'BIND_PHONE', // 绑定手机
  FORCE_KYC: 'FORCE_KYC', // 强制 KYC
  SIGN_CONTRACT: 'SIGN_CONTRACT', // 签订合同
  CUSTOM_NOTICE: 'CUSTOM_NOTICE', // 自定义通知
  SPECIAL_DEDUCTION: 'SPECIAL_DEDUCTION', // 特殊扣减
} as const;

export type ActionCode = (typeof ACTION_CODES)[keyof typeof ACTION_CODES] | string;

/**
 * 待办操作
 */
export interface PendingAction {
  id: string;
  userId: string;
  accountSequence?: string; // 用户账号序列号 (如 D25122700022)
  actionCode: string;
  actionParams: Record<string, unknown> | null;
  status: PendingActionStatus;
  priority: number;
  expiresAt: string | null;
  createdAt: string;
  completedAt: string | null;
  createdBy: string | null;
}

/**
 * 创建待办操作请求
 */
export interface CreatePendingActionRequest {
  userId: string;
  actionCode: string;
  actionParams?: Record<string, unknown>;
  priority?: number;
  expiresAt?: string;
}

/**
 * 批量创建待办操作请求
 */
export interface BatchCreatePendingActionRequest {
  userIds: string[];
  actionCode: string;
  actionParams?: Record<string, unknown>;
  priority?: number;
  expiresAt?: string;
}

/**
 * 更新待办操作请求
 */
export interface UpdatePendingActionRequest {
  actionParams?: Record<string, unknown>;
  status?: PendingActionStatus;
  priority?: number;
  expiresAt?: string | null;
}

/**
 * 查询待办操作参数
 */
export interface QueryPendingActionsParams {
  userId?: string;
  actionCode?: string;
  status?: PendingActionStatus;
  page?: number;
  limit?: number;
}

/**
 * 待办操作列表响应
 */
export interface PendingActionListResponse {
  items: PendingAction[];
  total: number;
  page: number;
  limit: number;
}

/**
 * 批量创建结果
 */
export interface BatchCreateResult {
  successCount: number;
  failedCount: number;
  failedUserIds?: string[];
}

/**
 * 操作代码选项（用于下拉选择）
 */
export const ACTION_CODE_OPTIONS = [
  { value: ACTION_CODES.ADOPTION_WIZARD, label: '认种向导' },
  { value: ACTION_CODES.SETTLE_REWARDS, label: '结算奖励' },
  { value: ACTION_CODES.BIND_PHONE, label: '绑定手机' },
  { value: ACTION_CODES.FORCE_KYC, label: '强制 KYC' },
  { value: ACTION_CODES.SIGN_CONTRACT, label: '签订合同' },
  { value: ACTION_CODES.CUSTOM_NOTICE, label: '自定义通知' },
  { value: ACTION_CODES.SPECIAL_DEDUCTION, label: '特殊扣减' },
] as const;

/**
 * 特殊扣减操作参数
 */
export interface SpecialDeductionParams {
  amount: number;      // 扣减金额 (0 表示全额线下结算模式)
  reason: string;      // 扣减原因/备注
  createdBy: string;   // 创建人
  isOfflineSettlement?: boolean;  // 是否为全额线下结算模式
}

/**
 * 全额线下结算扣减操作参数
 * 当 amount 为 0 时，系统会自动扣减用户所有未处理的结算收益
 */
export interface OfflineSettlementDeductionParams {
  isOfflineSettlement: true;  // 标记为全额线下结算模式
  reason: string;             // 扣减原因/备注 (默认: "该收益已由用户与上级间线下完成现金收付")
  createdBy: string;          // 创建人
}

/**
 * 状态选项（用于筛选和显示）
 */
export const STATUS_OPTIONS = [
  { value: 'PENDING', label: '待执行', color: '#faad14' },
  { value: 'COMPLETED', label: '已完成', color: '#52c41a' },
  { value: 'CANCELLED', label: '已取消', color: '#999999' },
] as const;

/**
 * 获取状态显示信息
 */
export function getStatusInfo(status: PendingActionStatus) {
  return STATUS_OPTIONS.find((s) => s.value === status) || STATUS_OPTIONS[0];
}

/**
 * 获取操作代码标签
 */
export function getActionCodeLabel(code: string): string {
  const option = ACTION_CODE_OPTIONS.find((o) => o.value === code);
  return option?.label || code;
}
