// Canonical statement types. Mirrors the CHECK constraint and the
// normalize_statement_type() SQL function in migration 0013 — keep them in sync.
export const STATEMENT_TYPES = [
  "credit_card",
  "transaction",
  "savings",
  "loan",
  "offset",
  "investment",
  "other",
] as const;

export type StatementType = (typeof STATEMENT_TYPES)[number];

export const STATEMENT_TYPE_LABELS: Record<StatementType, string> = {
  credit_card: "Credit Card",
  transaction: "Transaction",
  savings: "Savings",
  loan: "Loan",
  offset: "Offset",
  investment: "Investment",
  other: "Other",
};

export function isStatementType(v: string | null | undefined): v is StatementType {
  return !!v && (STATEMENT_TYPES as readonly string[]).includes(v);
}

/** Falls back to "other" for anything unrecognised (e.g. rows predating 0013). */
export function asStatementType(v: string | null | undefined): StatementType {
  return isStatementType(v) ? v : "other";
}

export function formatStatementType(v: string | null | undefined): string {
  return STATEMENT_TYPE_LABELS[asStatementType(v)];
}

/**
 * Types where the headline figure is "amount owed" rather than "balance held",
 * and where a payment due date is meaningful.
 */
export function isLiability(v: string | null | undefined): boolean {
  const t = asStatementType(v);
  return t === "credit_card" || t === "loan";
}
