// Client-side copy of the account type/subtype matrix — Task 2 of
// docs/superpowers/plans/2026-08-02-chart-management.md. The server's
// server/src/templates/types.ts SUBTYPES_BY_TYPE (mirroring migration 0016's
// accounts_subtype_valid_for_type CHECK) is the authority; this module only
// exists because web/ can't import server/ source. If the matrix ever
// changes, change it there first — the API refuses an invalid pairing with
// its own English either way, so a drift here degrades to a wasted round
// trip, never a wrong write.
//
// Labels are the STANDARD accounting terms (CLAUDE.md §9 as amended
// 2026-08-02): section headers say "Credit cards" and "Accounts receivable",
// not "Other" — a credit card must be findable as a credit card.

export type LedgerType = "asset" | "liability" | "equity" | "income" | "expense";

export type AccountSubtype =
  | "bank" | "accounts-receivable" | "other-current-asset" | "fixed-asset" | "other-asset"
  | "credit-card" | "line-of-credit" | "accounts-payable" | "other-current-liability" | "long-term-liability"
  | "equity"
  | "income" | "other-income"
  | "expense" | "cost-of-goods-sold" | "other-expense";

export const LEDGER_TYPES: readonly LedgerType[] = [
  "asset", "liability", "equity", "income", "expense",
];

/** Subtypes valid for each type, in the chart screen's SECTION display order
 * (spec Part 2: Assets = Bank accounts first … Expenses = COGS first). */
export const SUBTYPES_BY_TYPE: Readonly<Record<LedgerType, readonly AccountSubtype[]>> = {
  asset: ["bank", "accounts-receivable", "other-current-asset", "fixed-asset", "other-asset"],
  liability: [
    "credit-card", "line-of-credit", "accounts-payable",
    "other-current-liability", "long-term-liability",
  ],
  equity: ["equity"],
  income: ["income", "other-income"],
  expense: ["cost-of-goods-sold", "expense", "other-expense"],
};

/** Section-header label per subtype — plural standard terms. */
export const SUBTYPE_LABELS: Readonly<Record<AccountSubtype, string>> = {
  "bank": "Bank accounts",
  "accounts-receivable": "Accounts receivable",
  "other-current-asset": "Other current assets",
  "fixed-asset": "Fixed assets",
  "other-asset": "Other assets",
  "credit-card": "Credit cards",
  "line-of-credit": "Lines of credit",
  "accounts-payable": "Accounts payable",
  "other-current-liability": "Other current liabilities",
  "long-term-liability": "Long-term liabilities",
  "equity": "Equity",
  "income": "Income",
  "other-income": "Other income",
  "expense": "Expenses",
  "cost-of-goods-sold": "Cost of goods sold",
  "other-expense": "Other expenses",
};

/** Top-level heading per type — plural standard terms. */
export const TYPE_HEADINGS: Readonly<Record<LedgerType, string>> = {
  asset: "Assets",
  liability: "Liabilities",
  equity: "Equity",
  income: "Income",
  expense: "Expenses",
};

/** Singular labels for the add-account modal's type select. */
export const TYPE_LABELS_SINGULAR: Readonly<Record<LedgerType, string>> = {
  asset: "Asset",
  liability: "Liability",
  equity: "Equity",
  income: "Income",
  expense: "Expense",
};

/** The add-account modal's default subtype per type — deliberately the most
 * GENERIC one (a user picks bank/credit-card explicitly; defaulting to bank
 * would silently file every quick add under Bank accounts). */
export const DEFAULT_SUBTYPE: Readonly<Record<LedgerType, AccountSubtype>> = {
  asset: "other-current-asset",
  liability: "other-current-liability",
  equity: "equity",
  income: "income",
  expense: "expense",
};

/** The shape every /api/c/accounts row shares (server/src/accounts.ts).
 * `balance` (withBalances=1 only) is natural-signed: a credit-normal account
 * in good standing reads positive. */
export interface ApiAccount {
  id: string;
  code: string;
  name: string;
  type: LedgerType;
  subtype: AccountSubtype | null;
  active: boolean;
  balance?: string;
}
