CREATE TABLE IF NOT EXISTS postgres_monitor_snapshot (
    id TEXT PRIMARY KEY,
    collected_at TIMESTAMPTZ NOT NULL,
    payload_json JSONB NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_postgres_monitor_snapshot_collected
    ON postgres_monitor_snapshot (collected_at DESC);

CREATE TABLE IF NOT EXISTS postgres_maintenance_audit (
    id TEXT PRIMARY KEY,
    tenant_id TEXT,
    actor_prefix TEXT,
    kind TEXT NOT NULL,
    target_table TEXT,
    dry_run BOOLEAN NOT NULL DEFAULT false,
    status TEXT NOT NULL,
    detail_json JSONB,
    error_message TEXT,
    created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    started_at TIMESTAMPTZ,
    finished_at TIMESTAMPTZ,
    CONSTRAINT postgres_maintenance_audit_status_chk CHECK (
        status IN ('queued', 'running', 'succeeded', 'failed', 'cancelled')
    )
);

CREATE INDEX IF NOT EXISTS idx_postgres_maintenance_audit_created
    ON postgres_maintenance_audit (created_at DESC);
