-- NEO-146: append-only contract completion outcome audit log.
CREATE TABLE IF NOT EXISTS contract_outcome (
    id TEXT NOT NULL PRIMARY KEY,
    contract_instance_id TEXT NOT NULL REFERENCES contract_instance(contract_instance_id) ON DELETE CASCADE,
    player_id TEXT NOT NULL REFERENCES player_position(player_id) ON DELETE CASCADE,
    idempotency_key TEXT NOT NULL UNIQUE,
    granted_items JSONB NOT NULL DEFAULT '[]'::jsonb,
    granted_skill_xp JSONB NOT NULL DEFAULT '[]'::jsonb,
    granted_reputation JSONB NOT NULL DEFAULT '[]'::jsonb,
    recorded_at TIMESTAMPTZ NOT NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS idx_contract_outcome_instance_recorded
    ON contract_outcome (contract_instance_id, recorded_at, id);

COMMENT ON TABLE contract_outcome IS 'Append-only contract completion outcome audit (NEO-146).';
