import type { SettlementBillListScope } from "@/api/admin-agent-settlement";

export type SettlementPeriodView = "pending" | "bills" | "ledger";

export type SettlementBillScopeFilter = "all" | SettlementBillListScope;

const VALID_VIEWS: SettlementPeriodView[] = ["pending", "bills", "ledger"];

const VALID_BILL_SCOPES: SettlementBillScopeFilter[] = [
  "all",
  "pending_confirm",
  "awaiting_payment",
  "settled",
  "adjustment",
];

function parsePositiveInt(raw: string | null): number | null {
  if (raw === null || raw === "") {
    return null;
  }
  const value = Number(raw);
  return Number.isInteger(value) && value > 0 ? value : null;
}

export function settlementCenterListHref(adminSiteId?: number | null): string {
  return settlementCenterScopeHref(adminSiteId, "all");
}

/** 结算中心列表深链，可带账单筛选 scope（待确认 / 待收付等）。 */
export function settlementCenterScopeHref(
  adminSiteId?: number | null,
  billScope: SettlementBillScopeFilter = "all",
): string {
  const params = new URLSearchParams();
  if (adminSiteId != null && adminSiteId > 0) {
    params.set("site", String(adminSiteId));
  }
  if (billScope !== "all") {
    params.set("scope", billScope);
  }
  const qs = params.toString();
  return qs ? `/admin/settlement-center?${qs}` : "/admin/settlement-center";
}

export function settlementPeriodViewHref(
  periodId: number,
  view: SettlementPeriodView = "pending",
  adminSiteId?: number | null,
  billScope?: SettlementBillScopeFilter | null,
): string {
  const params = new URLSearchParams({
    period: String(periodId),
    view,
  });
  if (adminSiteId != null && adminSiteId > 0) {
    params.set("site", String(adminSiteId));
  }
  if (billScope != null && billScope !== "all") {
    params.set("scope", billScope);
  }
  return `/admin/settlement-center?${params.toString()}`;
}

export function preferredBillScopeForPeriod(summary?: {
  pending_confirm?: number;
  awaiting_payment?: number;
}): SettlementBillScopeFilter {
  const pending = summary?.pending_confirm ?? 0;
  const awaiting = summary?.awaiting_payment ?? 0;
  if (pending > 0) {
    return "pending_confirm";
  }
  if (awaiting > 0) {
    return "awaiting_payment";
  }
  return "all";
}

export function parseSettlementCenterView(
  siteRaw: string | null,
  periodRaw: string | null,
  viewRaw: string | null,
  scopeRaw?: string | null,
): {
  siteId: number | null;
  periodId: number | null;
  view: SettlementPeriodView;
  billScope: SettlementBillScopeFilter;
} {
  const normalizedView =
    viewRaw === "reports" || viewRaw === "operations" ? "bills" : viewRaw;
  const view =
    normalizedView !== null && VALID_VIEWS.includes(normalizedView as SettlementPeriodView)
      ? (normalizedView as SettlementPeriodView)
      : "pending";

  const billScope =
    scopeRaw !== null &&
    scopeRaw !== "" &&
    VALID_BILL_SCOPES.includes(scopeRaw as SettlementBillScopeFilter)
      ? (scopeRaw as SettlementBillScopeFilter)
      : "all";

  return {
    siteId: parsePositiveInt(siteRaw),
    periodId: parsePositiveInt(periodRaw),
    view,
    billScope,
  };
}

export function isSettlementPeriodView(value: string): value is SettlementPeriodView {
  return VALID_VIEWS.includes(value as SettlementPeriodView);
}
