// lib/accounting/core/accounting-event-registry.ts

import type { PostingContext } from './context-types';
import type { JournalEntry } from './journal-types';

export enum AccountingEventType {
  PURCHASE_POSTED = 'PURCHASE_POSTED',
  PURCHASE_REVERSED = 'PURCHASE_REVERSED',
  SUPPLIER_PAYMENT_POSTED = 'SUPPLIER_PAYMENT_POSTED',
  SUPPLIER_PAYMENT_REVERSED = 'SUPPLIER_PAYMENT_REVERSED',
  PURCHASE_RETURN_POSTED = 'PURCHASE_RETURN_POSTED',
  GRNI_RECOGNIZED = 'GRNI_RECOGNIZED',
  GRNI_SETTLED = 'GRNI_SETTLED',
  FX_REVALUATION = 'FX_REVALUATION',
  // ...future events
}

export interface AccountingEventSchema<T = unknown> {
  type: AccountingEventType;
  version: string; // postingRuleVersion
  validate(payload: T): ValidationResult;
  handler(payload: T, context: PostingContext): Promise<PostingResult>;
  reversalStrategy?: (original: T) => T;
}

export interface ValidationResult {
  valid: boolean;
  errors?: string[];
}

export interface PostingResult {
  success: boolean;
  journalId?: string;
  error?: string;
  journal?: JournalEntry;
}
