export type Direction = 'income' | 'expense' | 'transfer' | 'refund' | 'fee';

export interface LedgerFile { path: string; content: string; }
export interface Account { name: string; openedOn: string; closedOn?: string; currencies: string[]; }
export interface Posting {
  account: string;
  amount?: string;
  currency?: string;
  cost?: string;          // 成本价 {...} 或 @ 单价（解析后归一化为 cost 字符串）
  price?: string;         // @ 价格
  metadata?: Record<string, string>;  // posting 级 metadata
}
export interface Transaction {
  id: string; date: string; flag: string; payee?: string; narration?: string;
  postings: Posting[]; tags: string[]; links: string[]; source: string; raw: string;
  metadata?: Record<string, string>;  // 交易级 metadata
}
/** balance assertion：2026-01-31 balance Assets:Alipay 100.00 CNY */
export interface BalanceAssertion { date: string; account: string; amount: string; currency?: string; source: string; line: number; }
/** 解析出的 option 指令：option "title" "My Ledger" */
export interface Option { key: string; value: string; source: string; line: number; }
export interface Diagnostic { level: 'error' | 'warning'; file: string; line: number; message: string; }
export interface LedgerIndex {
  files: LedgerFile[]; accounts: Map<string, Account>; transactions: Transaction[];
  balances: BalanceAssertion[];        // balance 断言（只读，用于校验/诊断）
  options: Option[];                    // option 指令（如 operating_currency）
  diagnostics: Diagnostic[]; unsupported: Diagnostic[];
}
export interface TransactionDraft { date: string; payee?: string; narration: string; flag?: string; postings: Posting[]; tags?: string[]; links?: string[]; sourceEventIds?: string[]; metadata?: Record<string, string>; }
export interface ValidationResult { valid: boolean; errors: string[]; balances: Record<string, string>; }
export interface ImportedEvent {
  id: string; occurredAt: string; amount: string; currency: string; direction: Direction;
  counterparty: string; memo: string; externalId?: string; raw: Record<string, string>;
  /** 预填资金来源账户（手动/拍照入口提供，BillPipeline 分类时优先使用，跳过自动分类）。 */
  sourceAccount?: string;
  /** 预填分类账户（手动/拍照入口提供，BillPipeline 分类时优先使用，跳过自动分类）。 */
  categoryAccount?: string;
}
export interface Rule {
  id: string; priority: number; direction?: Direction; counterpartyContains?: string;
  memoContains?: string; minAmount?: string; maxAmount?: string; currency?: string;
  /** 否定条件：对手方包含此文本时规则不命中。 */
  counterpartyNotContains?: string;
  /** 否定条件：备注包含此文本时规则不命中。 */
  memoNotContains?: string;
  sourceAccount: string; categoryAccount: string; narration?: string; tags?: string[]; hits: number;
}
export interface Classification { draft: TransactionDraft; ruleId?: string; confidence: 'high' | 'low'; }
