import { DomainEvent, IEventBus } from '../subsystems/events/event-bus.protocol.js';
import '../types/drizzle.js';
import 'drizzle-orm/node-postgres';

/**
 * Lifecycle event emission for BaseService.
 *
 * Ported from pattern-stack/atoms/patterns/services/base.py — the Python
 * BaseService emits LIFECYCLE and CHANGE events on every CRUD operation.
 * This module provides the same capability for the TypeScript codegen stack.
 *
 * Design:
 *   - Fire-and-forget: event emission never fails the CRUD operation.
 *   - IEventBus is optional: if no EVENT_BUS is injected, emission is silently
 *     skipped. This means base classes work in projects that haven't installed
 *     the events subsystem.
 *   - LIFECYCLE events carry an entity snapshot in payload.
 *   - CHANGE events carry per-field old/new diffs.
 *   - Controlled per-entity via `emitLifecycleEvents` flag (default: true).
 *
 * @deprecated EVT-7 — Lifecycle events are untyped and emit outside of the
 *   CRUD transaction. New work should declare an `emits:` block on the entity
 *   and publish typed domain events from use-cases via TYPED_EVENT_BUS inside
 *   the same Drizzle transaction. See `docs/specs/EVT-7.md`. This helper is
 *   retained for BaseService backward compatibility until all entities have
 *   migrated to typed emits.
 */

type EventCategory = 'lifecycle' | 'change';
/**
 * Snapshot an entity's field values, excluding system fields.
 * Mirrors pattern-stack's `_get_entity_snapshot()`.
 */
declare function entitySnapshot(entity: Record<string, unknown>): Record<string, unknown>;
/**
 * Diff two entity snapshots, returning per-field old/new pairs.
 * Only includes fields that actually changed.
 */
declare function diffSnapshots(before: Record<string, unknown>, after: Record<string, unknown>): Array<{
    field: string;
    oldValue: unknown;
    newValue: unknown;
}>;
declare function buildLifecycleEvent(entityName: string, action: 'created' | 'updated' | 'deleted', entityId: string, snapshot?: Record<string, unknown>): DomainEvent;
declare function buildChangeEvents(entityName: string, entityId: string, changes: Array<{
    field: string;
    oldValue: unknown;
    newValue: unknown;
}>): DomainEvent[];
/**
 * Emit events to the bus, swallowing errors.
 * Mirrors pattern-stack's `_emit_lifecycle_event()` try/except.
 */
declare function emitSafely(eventBus: IEventBus | undefined, events: DomainEvent[]): Promise<void>;

export { type EventCategory, buildChangeEvents, buildLifecycleEvent, diffSnapshots, emitSafely, entitySnapshot };
