CREATE TABLE IF NOT EXISTS contract_timelines (
    tenant_key TEXT NOT NULL,
    contract_id TEXT NOT NULL,
    title TEXT NOT NULL,
    payload JSONB NOT NULL,
    receipt_id TEXT NOT NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    PRIMARY KEY (tenant_key, contract_id)
);

CREATE INDEX IF NOT EXISTS contract_timelines_tenant_updated_idx
    ON contract_timelines (tenant_key, updated_at DESC);

CREATE TABLE IF NOT EXISTS execution_receipts (
    tenant_key TEXT NOT NULL,
    receipt_id TEXT NOT NULL,
    skill_name TEXT NOT NULL,
    contract_id TEXT NOT NULL,
    status TEXT NOT NULL,
    payload JSONB NOT NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    PRIMARY KEY (tenant_key, receipt_id)
);

CREATE INDEX IF NOT EXISTS execution_receipts_tenant_contract_idx
    ON execution_receipts (tenant_key, contract_id, created_at DESC);
