CREATE TABLE IF NOT EXISTS contract_clock_contracts (
    tenant_key TEXT NOT NULL,
    contract_id TEXT NOT NULL,
    title TEXT NOT NULL,
    source_text_sha256 TEXT NOT NULL,
    deadlines JSONB NOT NULL,
    analysis JSONB 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_clock_contracts_tenant_updated_idx
    ON contract_clock_contracts (tenant_key, updated_at DESC);

CREATE TABLE IF NOT EXISTS contract_clock_execution_receipts (
    id BIGSERIAL PRIMARY KEY,
    tenant_key TEXT NOT NULL,
    contract_id TEXT NOT NULL,
    skill_name TEXT NOT NULL,
    task_id TEXT NOT NULL,
    input_hash TEXT NOT NULL,
    status TEXT NOT NULL,
    result_summary JSONB NOT NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS contract_clock_receipts_tenant_contract_idx
    ON contract_clock_execution_receipts (tenant_key, contract_id, created_at DESC);
