create schema if not exists building_blocks;
create schema if not exists signal_engine;

create table if not exists building_blocks.outbox_message (
    message_id uuid primary key,
    event_type text not null,
    schema_version integer not null check (schema_version > 0),
    payload_json jsonb not null,
    correlation_id text not null,
    occurred_at timestamptz not null,
    payload_hash text not null,
    published_at timestamptz null,
    attempt integer not null default 0
);

create table if not exists building_blocks.inbox_message (
    consumer text not null,
    message_id uuid not null,
    received_at timestamptz not null,
    payload_hash text not null,
    primary key (consumer, message_id)
);

create table if not exists building_blocks.job_run (
    job_run_id uuid primary key,
    job_type text not null,
    job_version integer not null,
    scope_key text not null,
    idempotency_key text not null unique,
    watermark text null,
    app_version text not null,
    model_version text null,
    config_version text null,
    data_version text null,
    contract_version text null,
    status text not null,
    input_hash text null,
    output_hash text null,
    started_at timestamptz null,
    heartbeat_at timestamptz null,
    finished_at timestamptz null,
    error_code text null,
    trace_id text null
);
