export function formatStatusWithRaw(
  status: string,
  labelMap: Record<string, string> = {}
) {
  const normalized = String(status || "").trim();

  if (!normalized) {
    return "-";
  }

  const key = normalized.toLowerCase();
  const label = labelMap[key];

  if (!label || label === normalized) {
    return normalized;
  }

  return `${label} (${normalized})`;
}

export function formatAuditAction(action: string) {
  const labelMap: Record<string, string> = {
    admin_user_created: "创建后台用户",
    admin_user_role_updated: "修改用户角色",
    admin_user_status_updated: "修改用户状态",
    admin_user_password_reset: "重置用户密码",
    task_release_inventory: "释放任务库存",
    task_release_inventory_binding: "释放任务库存绑定",
    task_regenerate_claim_link: "重发领取链接",
    task_closed: "关闭任务",
    task_mark_manual_review: "转人工处理",
    task_complete_manual_dispatch: "完成人工履约",
    inventory_item_released: "释放库存项",
    inventory_item_invalidated: "作废库存项",
    webhook_replayed: "重放 Webhook",
    platform_shop_config_updated: "更新店铺配置",
    platform_fulfillment_config_updated: "更新履约配置",
  };

  return formatStatusWithRaw(action, labelMap);
}

export function formatAuditTargetType(targetType: string) {
  const labelMap: Record<string, string> = {
    admin_user: "后台用户",
    task: "交付任务",
    task_inventory_binding: "任务库存绑定",
    inventory_item: "库存项",
    webhook_event: "Webhook 事件",
    platform_config: "平台配置",
  };

  return formatStatusWithRaw(targetType, labelMap);
}

export function formatTaskEventType(eventType: string) {
  const labelMap: Record<string, string> = {
    manual_dispatch_completed: "人工履约已回写",
    inventory_binding_released: "库存绑定已释放",
    claim_redeem_code_used: "兑换码已使用",
    claim_redeem_code_invalid: "兑换码错误",
    claim_redeem_inventory_replaced: "已切换新兑换码",
    claim_redeem_completed: "兑换链路完成",
    agiso_auto_delivery_success: "咸鱼自动发货成功",
    agiso_auto_delivery_failed: "咸鱼自动发货失败",
    agiso_auto_delivery_skipped: "咸鱼自动发货已跳过",
  };

  return formatStatusWithRaw(eventType, labelMap);
}

export function formatRedeemOutcomeLabel(value: string) {
  const normalized = String(value || "").trim();

  switch (normalized) {
    case "success":
      return "兑换成功";
    case "code_used":
      return "兑换码已使用";
    case "code_invalid":
      return "兑换码错误";
    case "captcha_rejected":
      return "验证码错误";
    case "failed":
      return "兑换失败";
    default:
      return normalized || "-";
  }
}

export function formatRedeemResolutionStatus(value: string) {
  return value === "success"
    ? "已完成"
    : value === "failed"
    ? "失败收口"
    : value || "-";
}

export function formatAutoDeliveryTrigger(value: string) {
  const normalized = String(value || "").trim();

  switch (normalized) {
    case "claim_redeemed":
      return "用户兑换完成后触发";
    case "manual_dispatch_completed":
      return "人工履约完成后触发";
    case "task_delivered":
      return "任务交付完成后触发";
    default:
      return normalized || "-";
  }
}

export function formatAutoDeliveryReason(value: string) {
  const normalized = String(value || "").trim();

  switch (normalized) {
    case "waiting_other_tasks":
      return "订单下仍有任务未完成交付";
    case "already_sent":
      return "该订单已经执行过自动发货";
    case "auto_delivery_disabled":
      return "自动发货开关已关闭";
    case "missing_config":
      return "缺少 Agiso 自动发货配置";
    case "request_failed":
      return "Agiso 接口返回失败";
    case "request_error":
      return "请求 Agiso 接口时发生异常";
    case "delivery_not_confirmed":
      return "接口返回成功，但订单状态未确认进入已发货";
    case "task_not_delivered":
      return "当前任务尚未进入已交付状态";
    case "not_supported":
      return "当前订单不支持 Agiso 咸鱼自动发货";
    default:
      return normalized || "-";
  }
}

export function formatKuaishouRoleInfoLabel(value: string) {
  const normalized = String(value || "").trim();

  switch (normalized) {
    case "name":
      return "角色名称";
    case "rid":
      return "角色 ID";
    default:
      return normalized || "-";
  }
}

export function formatTaskEventPayload(payload: Record<string, unknown>) {
  const parts = [
    payload.outcome ? `结果 ${payload.outcome}` : "",
    payload.resultCode ? `代码 ${payload.resultCode}` : "",
    payload.resultMessage ? `说明 ${payload.resultMessage}` : "",
    payload.codeMasked ? `凭据 ${payload.codeMasked}` : "",
    payload.previousCodeMasked ? `旧码 ${payload.previousCodeMasked}` : "",
    payload.nextCodeMasked ? `新码 ${payload.nextCodeMasked}` : "",
    payload.inventoryItemId ? `库存项 #${payload.inventoryItemId}` : "",
    payload.previousInventoryItemId
      ? `旧库存 #${payload.previousInventoryItemId}`
      : "",
    payload.nextInventoryItemId ? `新库存 #${payload.nextInventoryItemId}` : "",
    payload.ticketCodeMasked ? `卡券号 ${payload.ticketCodeMasked}` : "",
    payload.vnId ? `虚拟号 #${payload.vnId}` : "",
    payload.vnPhoneMasked ? `号码 ${payload.vnPhoneMasked}` : "",
    payload.bindUrl ? `绑定链接 ${payload.bindUrl}` : "",
    payload.sendType ? `发送类型 ${payload.sendType}` : "",
    payload.note ? `备注 ${payload.note}` : "",
    payload.previousOutcome
      ? `切换原因 ${formatRedeemOutcomeLabel(
          String(payload.previousOutcome || "")
        )}`
      : "",
    payload.deliveryReference ? `单号 ${payload.deliveryReference}` : "",
    payload.platformOrderId ? `平台单 ${payload.platformOrderId}` : "",
    payload.trigger
      ? `触发 ${formatAutoDeliveryTrigger(String(payload.trigger || ""))}`
      : "",
    payload.reason ? `原因 ${payload.reason}` : "",
    payload.purchaseTriggered === true ? "已触发购买" : "",
    payload.usedKnapsack === true ? "使用背包库存" : "",
    payload.responseStatus ? `HTTP ${payload.responseStatus}` : "",
    payload.requestId ? `请求 ${payload.requestId}` : "",
    payload.errorMessage ? `错误 ${payload.errorMessage}` : "",
  ].filter(Boolean);

  if (parts.length > 0) {
    return parts.join(" · ");
  }

  return JSON.stringify(payload || {});
}

export function formatBindingRoleSummary(
  roleKeys: string[] | undefined | null,
  emptyLabel = "-"
) {
  if (!Array.isArray(roleKeys) || roleKeys.length === 0) return emptyLabel;
  return roleKeys.join(" / ");
}

export function formatBindingCountSummary(summary: {
  totalBindingCount: number;
  reservedBindingCount: number;
  consumedBindingCount: number;
  releasedBindingCount: number;
}) {
  return `共 ${summary.totalBindingCount} 条 · 预占 ${summary.reservedBindingCount} · 已消费 ${summary.consumedBindingCount} · 已释放 ${summary.releasedBindingCount}`;
}

export function resolveTaskAutoDeliveryStatus(task: {
  agisoAutoDelivery?: { status?: string } | null;
  deliveryStatus: string;
}) {
  if (task.agisoAutoDelivery?.status) return task.agisoAutoDelivery.status;
  return task.deliveryStatus === "delivered" ? "pending" : "waiting";
}

export function formatTaskAutoDeliveryReason(task: {
  agisoAutoDelivery?: { errorMessage?: string; reason?: string } | null;
  deliveryStatus: string;
}) {
  if (task.agisoAutoDelivery?.errorMessage)
    return task.agisoAutoDelivery.errorMessage;
  if (task.agisoAutoDelivery?.reason)
    return formatAutoDeliveryReason(task.agisoAutoDelivery.reason);
  return task.deliveryStatus === "delivered"
    ? "当前任务已交付，等待订单统一自动发货"
    : "当前任务尚未完成交付";
}
