import { EventLogStatus, EventLogSource } from '@kici-dev/engine';
import type { Kysely } from 'kysely';
import type { Database } from '../db/types.js';
import type { LogStorage } from '../reporting/log-storage.js';
import type { WebhookInfo } from './handler.js';
/** Configuration for the writer. */
export interface EventLogWriterOptions {
    /** Soft cap in bytes for payload upload (default 5 MB). */
    maxPayloadBytes: number;
}
/**
 * Outcome metadata recorded alongside the delivery row.
 *
 * Most fields are optional and default to safe values — callers in the early
 * branches (duplicate, dedup) only know the routing-level metadata; later
 * branches (processed, lockfile_missing, failed) carry richer outcome data.
 */
export interface EventLogOutcome {
    /** Resolved tenant ID. Required (the table is org-scoped). */
    orgId: string;
    /** Where the delivery arrived. */
    source: EventLogSource;
    /** Outcome status. */
    status: EventLogStatus;
    /** Workflows matched by trigger evaluation (default 0). */
    matchedCount?: number;
    /** owner/repo extracted from payload (when available). */
    repoIdentifier?: string | null;
    /** Best-effort ref/branch (when extractable). */
    ref?: string | null;
    /** First run spawned by this delivery. */
    runId?: string | null;
    /** Failure reason when status='failed'. */
    errorMessage?: string | null;
}
/**
 * Pre-computed payload representation. Letting callers serialize once and
 * pass both the bytes and the JSON shape avoids double-stringify in the
 * hot path. The writer hashes the bytes, gzips them, and uploads.
 */
export interface PayloadBytes {
    /** UTF-8 encoded raw body bytes (what Platform hashed). */
    raw: Buffer;
}
/**
 * Build a `PayloadBytes` from the parsed webhook payload object. Uses
 * `JSON.stringify` with no formatting — same shape Platform uses to compute
 * its hash on the relay path (Platform actually hashes the inbound HTTP body
 * directly; for relay-replayed webhooks the orchestrator only ever sees the
 * parsed object, so we re-stringify deterministically and hash that).
 *
 * For DIRECT ingress paths (orchestrator HTTP endpoint) callers SHOULD pass
 * the raw HTTP body buffer instead — that matches Platform's hash byte-for-
 * byte for the same delivery in mixed-mode setups.
 */
export declare function payloadFromObject(payload: unknown): PayloadBytes;
/**
 * Build a `PayloadBytes` from a raw HTTP body string (direct ingress).
 */
export declare function payloadFromRawBody(body: string): PayloadBytes;
export declare class EventLogWriter {
    private readonly db;
    private readonly logStorage;
    private readonly opts;
    constructor(db: Kysely<Database>, logStorage: LogStorage, opts: EventLogWriterOptions);
    /**
     * Build the canonical object-storage key for a delivery's payload.
     * Exposed so the cleanup job can compute keys for deletion without
     * calling back into the writer instance.
     */
    static payloadKey(orgId: string, deliveryId: string): string;
    /**
     * Record a delivery outcome.
     *
     * Idempotent on `(org_id, delivery_id)`: if a row already exists (e.g. the
     * pipeline already wrote a `received` row before this `processed` follow-up),
     * the existing row is updated with the new outcome metadata. The
     * payload_key / hash / size fields are NOT overwritten on update — those
     * come from the original delivery's body.
     */
    record(info: WebhookInfo, payload: PayloadBytes, outcome: EventLogOutcome): Promise<void>;
}
//# sourceMappingURL=event-log.d.ts.map