-- 2026-04-27 P3.2.2 by Claude — Provider 版本歷史表
-- 功能：記錄每次 AI Provider 版本探測結果，偵測版本變更
-- 回滾：p3_2_provider_version_history_rollback.sql
BEGIN;

CREATE TABLE IF NOT EXISTS ai_provider_version_history (
    id          SERIAL PRIMARY KEY,
    provider    VARCHAR(40)  NOT NULL,
    model       VARCHAR(100) NOT NULL,
    version     VARCHAR(200),
    digest      VARCHAR(80),
    captured_at TIMESTAMPTZ  NOT NULL DEFAULT now(),
    prev_version VARCHAR(200),
    changed     BOOLEAN      NOT NULL DEFAULT FALSE
);

COMMIT;

-- CREATE INDEX CONCURRENTLY 不能在 transaction block 內執行
CREATE INDEX CONCURRENTLY IF NOT EXISTS ix_provider_version_captured
    ON ai_provider_version_history (provider, captured_at DESC);

CREATE INDEX CONCURRENTLY IF NOT EXISTS ix_provider_version_changed
    ON ai_provider_version_history (changed, captured_at DESC)
    WHERE changed = TRUE;
