import type { TFunction } from "i18next";

export function jobStatusLabel(status: string, t: TFunction): string {
  switch (status) {
    case "completed":
      return t("statusCompleted");
    case "running":
      return t("statusRunning");
    case "failed":
      return t("statusFailed");
    default:
      return status;
  }
}

export function itemStatusLabel(status: string, t: TFunction): string {
  switch (status) {
    case "mismatch":
      return t("itemMismatch");
    case "matched":
      return t("itemMatched");
    case "pending_check":
      return t("itemPendingCheck");
    case "stale_processing":
      return t("itemStaleProcessing");
    case "pending_reconcile":
      return t("itemPendingReconcile");
    case "missing_wallet_txn":
      return t("itemMissingWalletTxn");
    case "unexpected_wallet_txn":
      return t("itemUnexpectedWalletTxn");
    case "missing_refund":
      return t("itemMissingRefund");
    case "missing_reversal":
      return t("itemMissingReversal");
    case "main_site_record_missing":
      return t("itemMainSiteRecordMissing");
    case "main_site_failed":
      return t("itemMainSiteFailed");
    default:
      return status;
  }
}

export function mainSiteCheckLabel(status: string | null | undefined, t: TFunction): string {
  switch (status) {
    case "matched":
      return t("mainSiteMatched");
    case "not_found":
      return t("mainSiteNotFound");
    case "failed_on_main":
      return t("mainSiteFailed");
    case "unavailable":
      return t("mainSiteUnavailable");
    default:
      return t("mainSiteSkipped");
  }
}

/** 与钱包转账单列表 {@see wallet-console statusLabelT} 一致 */
export function transferOrderStatusLabel(status: string | null | undefined, t: TFunction): string {
  switch (status) {
    case "processing":
      return t("statusProcessing", { ns: "wallet" });
    case "success":
      return t("statusSuccess", { ns: "wallet" });
    case "failed":
      return t("statusFailed", { ns: "wallet" });
    case "pending_reconcile":
      return t("statusPendingReconcile", { ns: "wallet" });
    case "reversed":
      return t("statusReversed", { ns: "wallet" });
    case "manually_processed":
      return t("statusCaseClosed", { ns: "wallet" });
    default:
      return status?.trim() ? status : "—";
  }
}

export function itemCurrentStatusLabel(
  row: {
    current_transfer_status?: string | null;
    is_resolved?: boolean;
    resolved_at: string | null;
    status: string;
  },
  t: TFunction,
): string {
  const current = row.current_transfer_status?.trim();
  if (current) {
    return transferOrderStatusLabel(current, t);
  }
  if (row.is_resolved === true || row.resolved_at) {
    return t("itemResolved");
  }
  return itemStatusLabel(row.status, t);
}

export function itemResolutionLabel(
  row: { resolved_at: string | null; is_resolved?: boolean },
  t: TFunction,
): string {
  return row.is_resolved === true || row.resolved_at ? t("itemResolved") : t("itemUnresolved");
}

export function itemResolutionTone(
  row: { resolved_at: string | null; is_resolved?: boolean },
): "success" | "warning" {
  return row.is_resolved === true || row.resolved_at ? "success" : "warning";
}

export function getJobSummaryValue(
  summary: Record<string, unknown> | null | undefined,
  key: string,
): number {
  const raw = summary?.[key];
  return typeof raw === "number" && Number.isFinite(raw) ? raw : 0;
}

export function renderPeriodRange(
  row: { period_start: string | null; period_end: string | null },
  formatTs: (value: string | null | undefined) => string,
): string {
  const from = row.period_start ? formatTs(row.period_start) : "—";
  const to = row.period_end ? formatTs(row.period_end) : "—";
  return `${from} ~ ${to}`;
}

export function isOpenReconcileItem(row: { is_resolved?: boolean; resolved_at: string | null }): boolean {
  return row.is_resolved !== true && !row.resolved_at;
}