-- ============================================================
-- V5: 添加多AI服务提供商支持（修正版）
-- 描述: 支持接入多个AI服务提供商（OpenAI、RunningHub等）
-- 作者: 1818AI
-- 日期: 2025-10-20
-- ============================================================

-- 1. 扩展points_config表，添加服务商配置
ALTER TABLE `points_config`
ADD COLUMN IF NOT EXISTS `provider_type` VARCHAR(50) NOT NULL DEFAULT 'openai' 
    COMMENT 'AI服务提供商类型：openai, runninghub' AFTER `is_enabled`,
ADD COLUMN IF NOT EXISTS `provider_config` TEXT NULL 
    COMMENT '服务商特定配置（JSON格式）' AFTER `provider_type`;

-- 2. 扩展ai_task表，添加服务商相关字段
ALTER TABLE `ai_task`
ADD COLUMN IF NOT EXISTS `provider_type` VARCHAR(50) NULL 
    COMMENT 'AI服务提供商类型' AFTER `task_type`,
ADD COLUMN IF NOT EXISTS `provider_task_id` VARCHAR(100) NULL 
    COMMENT '服务商返回的任务ID' AFTER `provider_type`,
ADD COLUMN IF NOT EXISTS `provider_response` TEXT NULL 
    COMMENT '服务商原始响应（JSON）' AFTER `provider_task_id`;

-- 3. 添加索引以优化查询性能（如果不存在）
CREATE INDEX IF NOT EXISTS `idx_provider_task_id` ON `ai_task`(`provider_task_id`);
CREATE INDEX IF NOT EXISTS `idx_provider_type_status` ON `ai_task`(`provider_type`, `status`);

-- 4. 更新现有数据，设置默认provider_type为openai
UPDATE `ai_task` SET `provider_type` = 'openai' WHERE `provider_type` IS NULL;

-- 5. 插入RunningHub模型配置（文生视频 + 图生视频）
INSERT INTO `points_config` 
(model_name, points_cost, description, is_enabled, provider_type, provider_config, create_time, update_time) 
VALUES 
-- RunningHub Sora2 文生视频（webappId: 1973555977595301890）
('rh_sora2_text_portrait', 160, 'RunningHub Sora2 文生视频-竖屏（10秒）', 1, 'runninghub', 
 '{"webappId":"1973555977595301890","taskType":"text2video","model":"portrait","duration":10}', NOW(), NOW()),
 
('rh_sora2_text_landscape', 160, 'RunningHub Sora2 文生视频-横屏（10秒）', 1, 'runninghub', 
 '{"webappId":"1973555977595301890","taskType":"text2video","model":"landscape","duration":10}', NOW(), NOW()),
 
('rh_sora2_text_portrait_hd', 420, 'RunningHub Sora2 文生视频-高清竖屏（10秒）', 1, 'runninghub', 
 '{"webappId":"1973555977595301890","taskType":"text2video","model":"portrait-hd","duration":10}', NOW(), NOW()),
 
('rh_sora2_text_landscape_hd', 420, 'RunningHub Sora2 文生视频-高清横屏（10秒）', 1, 'runninghub', 
 '{"webappId":"1973555977595301890","taskType":"text2video","model":"landscape-hd","duration":10}', NOW(), NOW()),

-- RunningHub Sora2 图生视频（webappId: 1973555366057390081）
('rh_sora2_img_portrait', 180, 'RunningHub Sora2 图生视频-竖屏（10秒）', 1, 'runninghub', 
 '{"webappId":"1973555366057390081","taskType":"image2video","model":"portrait","duration":10}', NOW(), NOW()),
 
('rh_sora2_img_landscape', 180, 'RunningHub Sora2 图生视频-横屏（10秒）', 1, 'runninghub', 
 '{"webappId":"1973555366057390081","taskType":"image2video","model":"landscape","duration":10}', NOW(), NOW()),
 
('rh_sora2_img_portrait_hd', 480, 'RunningHub Sora2 图生视频-高清竖屏（10秒）', 1, 'runninghub', 
 '{"webappId":"1973555366057390081","taskType":"image2video","model":"portrait-hd","duration":10}', NOW(), NOW()),
 
('rh_sora2_img_landscape_hd', 480, 'RunningHub Sora2 图生视频-高清横屏（10秒）', 1, 'runninghub', 
 '{"webappId":"1973555366057390081","taskType":"image2video","model":"landscape-hd","duration":10}', NOW(), NOW()),

-- 15秒版本
('rh_sora2_text_portrait_15s', 260, 'RunningHub Sora2 文生视频-竖屏（15秒）', 1, 'runninghub', 
 '{"webappId":"1973555977595301890","taskType":"text2video","model":"portrait","duration":15}', NOW(), NOW()),
 
('rh_sora2_text_landscape_15s', 260, 'RunningHub Sora2 文生视频-横屏（15秒）', 1, 'runninghub', 
 '{"webappId":"1973555977595301890","taskType":"text2video","model":"landscape","duration":15}', NOW(), NOW()),

('rh_sora2_img_portrait_15s', 280, 'RunningHub Sora2 图生视频-竖屏（15秒）', 1, 'runninghub', 
 '{"webappId":"1973555366057390081","taskType":"image2video","model":"portrait","duration":15}', NOW(), NOW()),
 
('rh_sora2_img_landscape_15s', 280, 'RunningHub Sora2 图生视频-横屏（15秒）', 1, 'runninghub', 
 '{"webappId":"1973555366057390081","taskType":"image2video","model":"landscape","duration":15}', NOW(), NOW())
ON DUPLICATE KEY UPDATE 
    description = VALUES(description),
    points_cost = VALUES(points_cost),
    provider_type = VALUES(provider_type),
    provider_config = VALUES(provider_config),
    update_time = NOW();

-- 6. 验证插入结果
SELECT model_name, provider_type, points_cost, description 
FROM `points_config` 
WHERE `model_name` LIKE 'rh_sora2_%';

-- 7. 记录迁移日志
INSERT INTO `migration_log` (`version`, `description`, `executed_at`) 
VALUES ('V5', '添加多AI服务提供商支持（OpenAI、RunningHub）', NOW())
ON DUPLICATE KEY UPDATE `executed_at` = NOW();

