-- Migration: Add gemini_response_json and file_creation_status to ai_document_files table
-- Date: 2025-01-XX
-- Description: Adds columns to store Gemini API response JSON and track file creation status for batch processing

-- ===== Add Columns to AI Document Files Table =====
ALTER TABLE ai_document_files
ADD COLUMN gemini_response_json JSON DEFAULT NULL COMMENT 'Gemini APIのレスポンスJSON',
ADD COLUMN file_creation_status ENUM('pending', 'in_progress', 'completed', 'failed') DEFAULT 'pending' COMMENT 'ファイル作成ステータス',
ADD COLUMN majin_response TEXT DEFAULT NULL COMMENT 'MAJIN APIのレスポンス',
ADD COLUMN settings JSON DEFAULT NULL COMMENT 'スライド設定（MAJIN APIに送信する設定）',
ADD COLUMN error_message TEXT DEFAULT NULL COMMENT 'エラーメッセージ（失敗時）';

-- Add indexes for batch processing
ALTER TABLE ai_document_files
ADD INDEX idx_file_creation_status (file_creation_status),
ADD INDEX idx_file_creation_status_created (file_creation_status, created_at);

-- Update existing records to have 'completed' status if file_url is already set
UPDATE ai_document_files
SET file_creation_status = 'completed'
WHERE file_url IS NOT NULL AND file_url != '';

