/**
 * Postgres-backed {@link EventStore} for clawql-ouroboros lineages (#142).
 */
import { buildOntologyLineageFromEvents } from "./lineage-rebuild.js";
import { ensureOuroborosSchema } from "./postgres-pool.js";
export class PostgresOuroborosEventStore {
    pool;
    constructor(pool) {
        this.pool = pool;
    }
    async append(event) {
        await ensureOuroborosSchema();
        const ts = event.timestamp ?? new Date();
        await this.pool.query(`INSERT INTO clawql_ouroboros_events (root_seed_id, event_type, payload, created_at)
       VALUES ($1, $2, $3::jsonb, $4)`, [event.seed_id, event.type, JSON.stringify(event.data), ts]);
    }
    async getLineage(seedId) {
        await ensureOuroborosSchema();
        const res = await this.pool.query(`SELECT event_type, payload, created_at
       FROM clawql_ouroboros_events
       WHERE root_seed_id = $1
       ORDER BY id ASC`, [seedId]);
        const events = res.rows.map((row) => ({
            type: row.event_type,
            seed_id: seedId,
            data: row.payload,
            timestamp: row.created_at,
        }));
        return buildOntologyLineageFromEvents(seedId, events);
    }
}
