import type { JournalEntry, Invoice, Transaction, Customer, FiscalPeriod, DocumentAttachment, Receipt, CreditNote, ReconciliationMethod, InvoiceInboxItem, SupplierInvoice, } from '@/types' // ============================================================ // Core Event Types — discriminated union of all system events // ============================================================ export type CoreEvent = // Bookkeeping | { type: 'journal_entry.drafted'; payload: { entry: JournalEntry; userId: string } } | { type: 'journal_entry.committed'; payload: { entry: JournalEntry; userId: string } } | { type: 'journal_entry.corrected'; payload: { original: JournalEntry; storno: JournalEntry; corrected: JournalEntry; userId: string } } // Documents | { type: 'document.uploaded'; payload: { document: DocumentAttachment; userId: string } } // Invoicing | { type: 'invoice.created'; payload: { invoice: Invoice; userId: string } } | { type: 'invoice.sent'; payload: { invoice: Invoice; userId: string } } | { type: 'credit_note.created'; payload: { creditNote: CreditNote; userId: string } } // Banking | { type: 'transaction.synced'; payload: { transactions: Transaction[]; userId: string } } | { type: 'transaction.categorized'; payload: { transaction: Transaction; account: string; taxCode: string; userId: string } } | { type: 'transaction.reconciled'; payload: { transaction: Transaction; journalEntryId: string; method: ReconciliationMethod; userId: string } } // Periods | { type: 'period.locked'; payload: { period: FiscalPeriod; userId: string } } | { type: 'period.year_closed'; payload: { period: FiscalPeriod; userId: string } } // Customers | { type: 'customer.created'; payload: { customer: Customer; userId: string } } // Receipts | { type: 'receipt.extracted'; payload: { receipt: Receipt; documentId: string | null; confidence: number; userId: string; }} | { type: 'receipt.matched'; payload: { receipt: Receipt; transaction: Transaction; confidence: number; autoMatched: boolean; userId: string; }} | { type: 'receipt.confirmed'; payload: { receipt: Receipt; businessTotal: number; privateTotal: number; userId: string; }} // Supplier Invoice Lifecycle | { type: 'supplier_invoice.registered'; payload: { supplierInvoice: SupplierInvoice; userId: string } } | { type: 'supplier_invoice.approved'; payload: { supplierInvoice: SupplierInvoice; userId: string } } | { type: 'supplier_invoice.paid'; payload: { supplierInvoice: SupplierInvoice; paymentAmount: number; userId: string } } | { type: 'supplier_invoice.credited'; payload: { supplierInvoice: SupplierInvoice; creditNote: SupplierInvoice; userId: string } } // Supplier Invoice Inbox | { type: 'supplier_invoice.received'; payload: { inboxItem: InvoiceInboxItem; userId: string } } | { type: 'supplier_invoice.extracted'; payload: { inboxItem: InvoiceInboxItem; confidence: number; userId: string } } | { type: 'supplier_invoice.confirmed'; payload: { inboxItem: InvoiceInboxItem; supplierInvoice: SupplierInvoice; userId: string } } // ============================================================ // Helper Types // ============================================================ /** All possible event type strings */ export type CoreEventType = CoreEvent['type'] /** Extract the payload type for a given event type */ export type EventPayload = Extract['payload'] /** Handler function for a specific event type */ export type EventHandler = (payload: EventPayload) => Promise | void /** Subscription: event type + handler */ export interface EventSubscription { eventType: T handler: EventHandler }