import {
  DocumentType,
  type DocumentType as DocumentTypeCode,
} from "../../shared/api/enums";
import type { DeletedBySnapshot } from "./recycleBinTypes";

export const RESTORE_DESCRIPTIONS: Record<DocumentTypeCode, string> = {
  [DocumentType.MAIN]:
    "恢复主案时，系统将尝试恢复同一删除批次中仍然有效的权限和附件挂载；无效关系可能被跳过。",
  [DocumentType.SUB_PLAN]:
    "恢复子方案后，将继承所属主案当前最新权限。",
  [DocumentType.ATTACHMENT]:
    "恢复共享附件不会恢复历史挂载关系。",
};

export function toUtcIso(localDateTime: string): string | undefined {
  if (!localDateTime) return undefined;
  const date = new Date(localDateTime);
  return Number.isNaN(date.getTime()) ? undefined : date.toISOString();
}

export function formatLocalDate(value: string): string {
  return new Intl.DateTimeFormat("zh-CN", {
    year: "numeric",
    month: "2-digit",
    day: "2-digit",
    hour: "2-digit",
    minute: "2-digit",
    second: "2-digit",
    hour12: false,
  }).format(new Date(value));
}

export function deletedByLabel(value: DeletedBySnapshot | null): string {
  if (!value) return "删除人信息不可用";
  const identity =
    [value.realName, value.username ? `（${value.username}）` : null]
      .filter(Boolean)
      .join("") || "未知用户";
  return `${identity} · ${value.organizationName ?? "组织不可用"}`;
}

export const RESTORE_ERROR_MESSAGES: Record<string, string> = {
  INVALID_ARGUMENT: "恢复参数无效，请刷新回收站后重试。",
  FORBIDDEN: "当前账号无权访问或恢复回收站文档。",
  RESOURCE_NOT_FOUND: "该回收站记录不存在或已发生变化。",
  FILE_NOT_FOUND: "原始文件不存在，无法恢复，请联系管理员。",
  DATA_VERSION_CONFLICT: "记录已被其他操作修改，已重新加载最新回收站数据。",
  DOCUMENT_NOT_DELETED: "该文档当前不在回收站中。",
  FILE_INTEGRITY_MISMATCH: "文件完整性校验失败，无法恢复。",
  RESTORE_FILE_CHANGED: "恢复校验期间文件发生变化，未执行恢复。",
  RESTORE_CATEGORY_INVALID: "原分类已失效，无法恢复该文档。",
  RESTORE_PARENT_INVALID: "原所属主案不可用，无法恢复该子方案。",
  RESTORE_PERMISSION_INVALID: "恢复后无法形成有效权限配置。",
  RESTORE_RELATION_CONFLICT:
    "历史关系与当前有效关系冲突，不能强制覆盖恢复。",
  UNSUPPORTED_FILE_TYPE: "原始文件类型已不受支持，无法恢复。",
  FILE_PATH_INVALID: "系统无法安全定位原始文件，请联系管理员。",
  INTERNAL_ERROR: "系统处理恢复请求失败，请联系管理员。",
};
