-- ============================================================================
-- Phase 18: AuditLog 字段遷移
-- ============================================================================
-- 日期: 2026-03-28
-- 作者: Claude Code (首席架構師)
-- 原因: Phase 18 失敗自動修復閉環需要新字段，但未執行遷移導致 API 500
-- ============================================================================

-- 檢查並添加缺少的字段
-- 這些字段在 apps/api/src/db/models.py AuditLog 中定義但未遷移

-- 1. authorization_channel: 授權來源 (web, telegram, auto)
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'audit_logs' AND column_name = 'authorization_channel'
    ) THEN
        ALTER TABLE audit_logs ADD COLUMN authorization_channel VARCHAR(20);
        COMMENT ON COLUMN audit_logs.authorization_channel IS 'Authorization source: web, telegram, auto';
    END IF;
END $$;

-- 2. retry_count: 重試次數
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'audit_logs' AND column_name = 'retry_count'
    ) THEN
        ALTER TABLE audit_logs ADD COLUMN retry_count INTEGER DEFAULT 0 NOT NULL;
        COMMENT ON COLUMN audit_logs.retry_count IS 'Number of retry attempts';
    END IF;
END $$;

-- 3. failure_classification: 失敗分類
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'audit_logs' AND column_name = 'failure_classification'
    ) THEN
        ALTER TABLE audit_logs ADD COLUMN failure_classification VARCHAR(50);
        COMMENT ON COLUMN audit_logs.failure_classification IS 'Failure type: TIMEOUT, K8S_ERROR, NETWORK_ERROR, PERMISSION_DENIED';
    END IF;
END $$;

-- 4. source_approval_id: 原始 Approval ID (修復追蹤)
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'audit_logs' AND column_name = 'source_approval_id'
    ) THEN
        ALTER TABLE audit_logs ADD COLUMN source_approval_id VARCHAR(36);
        COMMENT ON COLUMN audit_logs.source_approval_id IS 'Original approval ID if this is a repair attempt';
        CREATE INDEX IF NOT EXISTS ix_audit_source_approval_id ON audit_logs(source_approval_id);
    END IF;
END $$;

-- 5. auto_repair_attempted: 是否嘗試自動修復
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'audit_logs' AND column_name = 'auto_repair_attempted'
    ) THEN
        ALTER TABLE audit_logs ADD COLUMN auto_repair_attempted BOOLEAN DEFAULT FALSE NOT NULL;
        COMMENT ON COLUMN audit_logs.auto_repair_attempted IS 'Whether auto-repair was attempted';
    END IF;
END $$;

-- 6. auto_repair_result: 自動修復結果
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'audit_logs' AND column_name = 'auto_repair_result'
    ) THEN
        ALTER TABLE audit_logs ADD COLUMN auto_repair_result TEXT;
        COMMENT ON COLUMN audit_logs.auto_repair_result IS 'Auto-repair result: AI analysis and repair outcome';
    END IF;
END $$;

-- 添加索引
CREATE INDEX IF NOT EXISTS ix_audit_authorization_channel ON audit_logs(authorization_channel);
CREATE INDEX IF NOT EXISTS ix_audit_failure_classification ON audit_logs(failure_classification);

-- 驗證遷移結果
SELECT
    column_name,
    data_type,
    is_nullable,
    column_default
FROM information_schema.columns
WHERE table_name = 'audit_logs'
ORDER BY ordinal_position;
