-- ============================================================
-- FOMS / KTV产线服务 → Sage (uapi + llmage + pricing) 注册SQL
-- 供应商: 开元云（北京）科技有限公司
-- 服务器: opencomputing.net GPU集群
-- ============================================================

SET @provider_id = 'kaiyuanyun';
SET @owner_org = 'foms_org_001';

-- ============================================================
-- 1. llmcatelog — 模型类目 (新增KTV专用类目)
-- ============================================================
INSERT IGNORE INTO llmcatelog (id, name, description) VALUES
('ktv_pipeline',     'KTV产线',         'KTV视频制作全流程：校准→合成→评估'),
('video_eval',       '视频评估',         'AI视频质量评估打分'),
('super_resolution', '超分辨率',         '图像/视频超分辨率增强'),
('asr',              '语音识别',         '自动语音识别转文字'),
('face_detect',      '人脸识别',         '人脸检测/识别/比对'),
('graph_process',    '图谱处理',         '知识图谱构建与查询'),
('ner',              '实体识别',         '命名实体识别'),
('song_eval',        '歌曲评分',         'AI歌曲质量评估'),
('music_gen',        '音乐生成',         'AI音乐/歌声合成'),
('audio_separate',   '音频分离',         '人声伴奏分离(Demucs)'),
('voice_convert',    '声音转换',         'RVC声音克隆/转换');

-- ============================================================
-- 2. llmprovider — 模型供应商
-- ============================================================
INSERT IGNORE INTO llmprovider (id, name) VALUES
('kaiyuanyun', '开元云（北京）科技有限公司');

-- ============================================================
-- 3. upapp — 上游应用 (每个服务一个upapp)
--    baseurl指向GPU服务器 opencomputing.net
-- ============================================================
INSERT IGNORE INTO upapp (id, name, baseurl, ownerid, auth_apiname) VALUES
('ktv-media-server',  'KTV媒体服务器',   'http://opencomputing.net:9080',  @owner_org, ''),
('ktv-video-eval',    'KTV视频评估',     'http://opencomputing.net:8901',  @owner_org, ''),
('ktv-realesrgan',    'KTV超分辨率',     'http://opencomputing.net:9082',  @owner_org, ''),
('ktv-asr',           'KTV语音识别',     'http://opencomputing.net:9925',  @owner_org, ''),
('ktv-face',          'KTV人脸识别',     'http://opencomputing.net:9091',  @owner_org, ''),
('ktv-graph',         'KTV图谱处理',     'http://opencomputing.net:9092',  @owner_org, ''),
('ktv-ner',           'KTV实体识别',     'http://opencomputing.net:9093',  @owner_org, ''),
('ktv-songrate',      'KTV歌曲评分',     'http://opencomputing.net:8900',  @owner_org, ''),
('ktv-synth',         'KTV音乐合成',     'http://opencomputing.net:9084',  @owner_org, ''),
('ktv-demucs',        'KTV音频分离',     'http://opencomputing.net:9083',  @owner_org, ''),
('ktv-rvc',           'KTV声音转换',     'http://opencomputing.net:9085',  @owner_org, '');

-- ============================================================
-- 4. llm — 模型定义
-- ============================================================
INSERT IGNORE INTO llm (id, name, model, providerid, ownerid, upappid, status) VALUES
('llm_ktv_pipeline',    'KTV产线流水线',    'ktv-media-server',    @provider_id, @owner_org, 'ktv-media-server',  'published'),
('llm_video_eval',      'KTV视频评估',       'ktv-video-eval',      @provider_id, @owner_org, 'ktv-video-eval',    'published'),
('llm_realesrgan',      'KTV超分辨率增强',    'ktv-realesrgan',      @provider_id, @owner_org, 'ktv-realesrgan',    'published'),
('llm_asr',             'KTV语音识别ASR',     'ktv-asr',             @provider_id, @owner_org, 'ktv-asr',           'published'),
('llm_face',            'KTV人脸识别',        'ktv-face',            @provider_id, @owner_org, 'ktv-face',          'published'),
('llm_graph',           'KTV图谱处理',        'ktv-graph',           @provider_id, @owner_org, 'ktv-graph',         'published'),
('llm_ner',             'KTV命名实体识别',     'ktv-ner',             @provider_id, @owner_org, 'ktv-ner',           'published'),
('llm_songrate',        'KTV歌曲评分',        'ktv-songrate',        @provider_id, @owner_org, 'ktv-songrate',      'published'),
('llm_synth',           'KTV音乐合成',        'ktv-synth',           @provider_id, @owner_org, 'ktv-synth',         'published'),
('llm_demucs',          'KTV人声伴奏分离',     'ktv-demucs',          @provider_id, @owner_org, 'ktv-demucs',        'published'),
('llm_rvc',             'KTV声音克隆转换',     'ktv-rvc',             @provider_id, @owner_org, 'ktv-rvc',           'published');

-- ============================================================
-- 5. uapi — API端点定义
--    name格式: {service}-{action}
-- ============================================================

-- 5.1 media-server APIs (12 endpoints)
INSERT IGNORE INTO uapi (name, upappid, path, httpmethod, headers, data, response, ioid) VALUES
('media-pipeline-submit',  'ktv-media-server', '/api/pipeline_submit/index.dspy',  'POST', '{"Content-Type":"application/json"}', '{"task_type":"{{task_type}}","params":{{json.dumps(params)}}}', '{"status":"ok","task_id":"{{task_id}}"}', NULL),
('media-pipeline-status',  'ktv-media-server', '/api/pipeline_status/index.dspy',  'GET',  NULL, NULL, '{"status":"{{status}}","progress":{{progress}}}', NULL),
('media-status',           'ktv-media-server', '/api/status/index.dspy',            'GET',  NULL, NULL, '{"status":"ok","gpu_available":{{gpu_available}}}', NULL),
('media-task',             'ktv-media-server', '/api/task/index.dspy',              'GET',  NULL, NULL, '{"task_id":"{{task_id}}","status":"{{status}}"}', NULL),
('media-submit',           'ktv-media-server', '/api/submit/index.dspy',            'POST', '{"Content-Type":"application/json"}', '{"video_url":"{{video_url}}","audio_url":"{{audio_url}}"}', '{"status":"ok"}', NULL),
('media-ktv',              'ktv-media-server', '/api/ktv/index.dspy',              'POST', '{"Content-Type":"application/json"}', '{"song_name":"{{song_name}}","lyrics":"{{lyrics}}"}', '{"status":"ok","video_url":"{{video_url}}"}', NULL),
('media-demucs',           'ktv-media-server', '/api/demucs/index.dspy',           'POST', '{"Content-Type":"application/json"}', '{"audio_url":"{{audio_url}}"}', '{"status":"ok","vocals_url":"{{vocals_url}}","accompaniment_url":"{{accompaniment_url}}"}', NULL),
('media-subtitle',         'ktv-media-server', '/api/subtitle/index.dspy',          'POST', '{"Content-Type":"application/json"}', '{"lyrics":"{{lyrics}}","audio_url":"{{audio_url}}"}', '{"status":"ok","subtitle_url":"{{subtitle_url}}"}', NULL),
('media-calibrate',        'ktv-media-server', '/api/calibrate/index.dspy',         'POST', '{"Content-Type":"application/json"}', '{"video_url":"{{video_url}}","audio_url":"{{audio_url}}"}', '{"status":"ok","offset":{{offset}}}', NULL),
('media-lyric-calibrate',  'ktv-media-server', '/api/lyric_calibrate/index.dspy',   'POST', '{"Content-Type":"application/json"}', '{"lyrics":"{{lyrics}}","audio_url":"{{audio_url}}"}', '{"status":"ok","calibrated_lyrics":"{{calibrated_lyrics}}"}', NULL),
('media-merge',            'ktv-media-server', '/api/merge/index.dspy',             'POST', '{"Content-Type":"application/json"}', '{"video_url":"{{video_url}}","audio_url":"{{audio_url}}"}', '{"status":"ok","output_url":"{{output_url}}"}', NULL),
('media-gpu-lock',         'ktv-media-server', '/api/gpu_lock/index.dspy',           'POST', '{"Content-Type":"application/json"}', '{"gpu_id":{{gpu_id}},"duration":{{duration}}}', '{"status":"ok","locked":true}', NULL);

-- 5.2 video-eval API
INSERT IGNORE INTO uapi (name, upappid, path, httpmethod, headers, data, response, ioid) VALUES
('video-eval-evaluate', 'ktv-video-eval', '/api/eval/index.dspy', 'POST', '{"Content-Type":"application/json"}', '{"video_url":"{{video_url}}"}', '{"status":"ok","score":{{score}},"details":{{json.dumps(details)}}}', NULL);

-- 5.3 realesrgan API
INSERT IGNORE INTO uapi (name, upappid, path, httpmethod, headers, data, response, ioid) VALUES
('realesrgan-upscale',  'ktv-realesrgan', '/api/submit/index.dspy', 'POST', '{"Content-Type":"application/json"}', '{"image_url":"{{image_url}}","scale":{{scale}}}', '{"status":"ok","output_url":"{{output_url}}"}', NULL),
('realesrgan-status',   'ktv-realesrgan', '/api/status/index.dspy', 'GET',  NULL, NULL, '{"status":"{{status}}","gpu_available":{{gpu_available}}}', NULL);

-- 5.4 asr API
INSERT IGNORE INTO uapi (name, upappid, path, httpmethod, headers, data, response, ioid) VALUES
('asr-transcribe', 'ktv-asr', '/api/transcribe/index.dspy', 'POST', '{"Content-Type":"application/json"}', '{"audio_url":"{{audio_url}}","language":"{{language}}"}', '{"status":"ok","text":"{{text}}","segments":{{json.dumps(segments)}}}', NULL);

-- 5.5 face API
INSERT IGNORE INTO uapi (name, upappid, path, httpmethod, headers, data, response, ioid) VALUES
('face-detect',    'ktv-face', '/api/detect',    'POST', '{"Content-Type":"application/json"}', '{"image_url":"{{image_url}}"}', '{"status":"ok","faces":{{json.dumps(faces)}}}', NULL),
('face-recognize', 'ktv-face', '/api/recognize', 'POST', '{"Content-Type":"application/json"}', '{"image_url":"{{image_url}}","face_id":"{{face_id}}"}', '{"status":"ok","identity":"{{identity}}","confidence":{{confidence}}}', NULL),
('face-compare',   'ktv-face', '/api/compare',   'POST', '{"Content-Type":"application/json"}', '{"image_url1":"{{url1}}","image_url2":"{{url2}}"}', '{"status":"ok","similarity":{{similarity}}}', NULL);

-- 5.6 graph API
INSERT IGNORE INTO uapi (name, upappid, path, httpmethod, headers, data, response, ioid) VALUES
('graph-neighbors', 'ktv-graph', '/api/graph/neighbors', 'GET', NULL, NULL, '{"status":"ok","nodes":{{json.dumps(nodes)}}}', NULL),
('graph-add-node',  'ktv-graph', '/api/graph/add_node',  'POST', '{"Content-Type":"application/json"}', '{"node_id":"{{node_id}}","attrs":{{json.dumps(attrs)}}}', '{"status":"ok"}', NULL),
('graph-add-edge',  'ktv-graph', '/api/graph/add_edge',  'POST', '{"Content-Type":"application/json"}', '{"from":"{{from}}","to":"{{to}}","label":"{{label}}"}', '{"status":"ok"}', NULL);

-- 5.7 ner API
INSERT IGNORE INTO uapi (name, upappid, path, httpmethod, headers, data, response, ioid) VALUES
('ner-extract', 'ktv-ner', '/api/extract', 'POST', '{"Content-Type":"application/json"}', '{"text":"{{text}}"}', '{"status":"ok","entities":{{json.dumps(entities)}}}', NULL);

-- 5.8 songrate API
INSERT IGNORE INTO uapi (name, upappid, path, httpmethod, headers, data, response, ioid) VALUES
('songrate-evaluate', 'ktv-songrate', '/api/evaluate', 'POST', '{"Content-Type":"application/json"}', '{"audio_url":"{{audio_url}}","scene":"{{scene}}"}', '{"status":"ok","score":{{score}},"details":{{json.dumps(details)}}}', NULL);

-- 5.9 ktv-synth API
INSERT IGNORE INTO uapi (name, upappid, path, httpmethod, headers, data, response, ioid) VALUES
('synth-generate', 'ktv-synth', '/api/generate/index.dspy', 'POST', '{"Content-Type":"application/json"}', '{"lyrics":"{{lyrics}}","style":"{{style}}","reference_audio":"{{reference_audio}}"}', '{"status":"ok","task_id":"{{task_id}}"}', NULL),
('synth-status',   'ktv-synth', '/api/status/index.dspy',   'GET',  NULL, NULL, '{"status":"{{status}}","audio_url":"{{audio_url}}"}', NULL);

-- 5.10 demucs API
INSERT IGNORE INTO uapi (name, upappid, path, httpmethod, headers, data, response, ioid) VALUES
('demucs-separate', 'ktv-demucs', '/api/separate/index.dspy', 'POST', '{"Content-Type":"application/json"}', '{"audio_url":"{{audio_url}}"}', '{"status":"ok","vocals_url":"{{vocals_url}}","accompaniment_url":"{{accompaniment_url}}"}', NULL);

-- 5.11 rvc API
INSERT IGNORE INTO uapi (name, upappid, path, httpmethod, headers, data, response, ioid) VALUES
('rvc-convert', 'ktv-rvc', '/api/convert', 'POST', '{"Content-Type":"application/json"}', '{"audio_url":"{{audio_url}}","voice_model":"{{voice_model}}"}', '{"status":"ok","output_url":"{{output_url}}"}', NULL);

-- ============================================================
-- 6. upappkey — API密钥 (服务间内部通信，使用统一密钥)
-- ============================================================
INSERT IGNORE INTO upappkey (id, upappid, apikey, secretkey) VALUES
('key_ktv_media',  'ktv-media-server',  'ktv-media-key-001',  ''),
('key_ktv_eval',   'ktv-video-eval',    'ktv-eval-key-001',   ''),
('key_ktv_esrgan', 'ktv-realesrgan',    'ktv-esrgan-key-001', ''),
('key_ktv_asr',    'ktv-asr',           'ktv-asr-key-001',    ''),
('key_ktv_face',   'ktv-face',          'ktv-face-key-001',   ''),
('key_ktv_graph',  'ktv-graph',         'ktv-graph-key-001',  ''),
('key_ktv_ner',    'ktv-ner',           'ktv-ner-key-001',    ''),
('key_ktv_song',   'ktv-songrate',      'ktv-song-key-001',   ''),
('key_ktv_synth',  'ktv-synth',         'ktv-synth-key-001',  ''),
('key_ktv_demucs', 'ktv-demucs',        'ktv-demucs-key-001', ''),
('key_ktv_rvc',    'ktv-rvc',           'ktv-rvc-key-001',    '');

-- ============================================================
-- 7. llm_api_map — 模型能力映射 (每个模型绑定一个默认类目+API)
-- ============================================================
INSERT IGNORE INTO llm_api_map (id, llmid, llmcatelogid, apiname, isdefaultcatelog) VALUES
-- media-server: 默认KTV产线，通用提交API
('map_ktv_pipeline', 'llm_ktv_pipeline', 'ktv_pipeline', 'media-pipeline-submit', '1'),
-- video-eval
('map_video_eval',   'llm_video_eval',   'video_eval',   'video-eval-evaluate',  '1'),
-- realesrgan
('map_realesrgan',   'llm_realesrgan',   'super_resolution', 'realesrgan-upscale', '1'),
-- asr
('map_asr',          'llm_asr',          'asr',          'asr-transcribe',       '1'),
-- face (多能力: detect默认)
('map_face_detect',  'llm_face',         'face_detect',  'face-detect',          '1'),
('map_face_recognize','llm_face',        'face_detect',  'face-recognize',       '0'),
-- graph
('map_graph',        'llm_graph',        'graph_process','graph-neighbors',      '1'),
-- ner
('map_ner',          'llm_ner',          'ner',          'ner-extract',          '1'),
-- songrate
('map_songrate',     'llm_songrate',     'song_eval',    'songrate-evaluate',    '1'),
-- ktv-synth
('map_synth',        'llm_synth',        'music_gen',    'synth-generate',       '1'),
-- demucs
('map_demucs',       'llm_demucs',       'audio_separate','demucs-separate',     '1'),
-- rvc
('map_rvc',          'llm_rvc',          'voice_convert', 'rvc-convert',         '1');

-- ============================================================
-- 8. pricing_program — 计价项目
-- ============================================================
INSERT IGNORE INTO pricing_program (id, name, ownerid, providerid, pricing_belong, discount, description) VALUES
('pp_ktv_pipeline',  'KTV产线流水线',      @owner_org, @provider_id, 'provider', 1.0, '按次计费，含校准+合成+评估全流程'),
('pp_video_eval',    'KTV视频评估',        @owner_org, @provider_id, 'provider', 1.0, '按评估视频时长计费'),
('pp_realesrgan',    'KTV超分辨率增强',     @owner_org, @provider_id, 'provider', 1.0, '按处理像素量计费'),
('pp_asr',           'KTV语音识别ASR',      @owner_org, @provider_id, 'provider', 1.0, '按音频时长计费'),
('pp_face',          'KTV人脸识别',        @owner_org, @provider_id, 'provider', 1.0, '按调用次数计费'),
('pp_graph',         'KTV图谱处理',        @owner_org, @provider_id, 'provider', 1.0, '按查询/写入次数计费'),
('pp_ner',           'KTV命名实体识别',     @owner_org, @provider_id, 'provider', 1.0, '按文本长度计费'),
('pp_songrate',      'KTV歌曲评分',        @owner_org, @provider_id, 'provider', 1.0, '按评估次数计费'),
('pp_synth',         'KTV音乐合成',        @owner_org, @provider_id, 'provider', 1.0, '按生成时长计费'),
('pp_demucs',        'KTV人声伴奏分离',     @owner_org, @provider_id, 'provider', 1.0, '按音频时长计费'),
('pp_rvc',           'KTV声音克隆转换',     @owner_org, @provider_id, 'provider', 1.0, '按转换时长计费');

-- ============================================================
-- 9. pricing_program_timing — 定价时段 (当前生效)
-- ============================================================
INSERT IGNORE INTO pricing_program_timing (id, ppid, name, pricing_data, enabled_date) VALUES
('ppt_ktv_pipeline',  'pp_ktv_pipeline',  '默认定价', 'unit_values:\n  次: 1\nfields:\n  price_factors:\n    type: string\n    role: factor\n    label: 计价因子\n  unit_prices:\n    type: float\n    role: factor\n    label: 单位定价(元)\n  unit:\n    type: string\n    role: factor\n    label: 计价单位\npricings:\n  - price_factors: per_call\n    unit_prices: 2.00\n    unit: 次', '2025-01-01'),

('ppt_video_eval',    'pp_video_eval',    '默认定价', 'unit_values:\n  秒: 1\nfields:\n  price_factors:\n    type: string\n    role: factor\n    label: 计价因子\n  unit_prices:\n    type: float\n    role: factor\n    label: 单位定价(元)\n  unit:\n    type: string\n    role: factor\n    label: 计价单位\npricings:\n  - price_factors: per_second\n    unit_prices: 0.05\n    unit: 秒', '2025-01-01'),

('ppt_realesrgan',    'pp_realesrgan',    '默认定价', 'unit_values:\n  次: 1\nfields:\n  price_factors:\n    type: string\n    role: factor\n    label: 计价因子\n  unit_prices:\n    type: float\n    role: factor\n    label: 单位定价(元)\n  unit:\n    type: string\n    role: factor\n    label: 计价单位\npricings:\n  - price_factors: per_call\n    unit_prices: 0.50\n    unit: 次', '2025-01-01'),

('ppt_asr',           'pp_asr',           '默认定价', 'unit_values:\n  秒: 1\nfields:\n  price_factors:\n    type: string\n    role: factor\n    label: 计价因子\n  unit_prices:\n    type: float\n    role: factor\n    label: 单位定价(元)\n  unit:\n    type: string\n    role: factor\n    label: 计价单位\npricings:\n  - price_factors: per_second\n    unit_prices: 0.01\n    unit: 秒', '2025-01-01'),

('ppt_face',          'pp_face',          '默认定价', 'unit_values:\n  次: 1\nfields:\n  price_factors:\n    type: string\n    role: factor\n    label: 计价因子\n  unit_prices:\n    type: float\n    role: factor\n    label: 单位定价(元)\n  unit:\n    type: string\n    role: factor\n    label: 计价单位\npricings:\n  - price_factors: per_call\n    unit_prices: 0.10\n    unit: 次', '2025-01-01'),

('ppt_graph',         'pp_graph',         '默认定价', 'unit_values:\n  次: 1\nfields:\n  price_factors:\n    type: string\n    role: factor\n    label: 计价因子\n  unit_prices:\n    type: float\n    role: factor\n    label: 单位定价(元)\n  unit:\n    type: string\n    role: factor\n    label: 计价单位\npricings:\n  - price_factors: per_call\n    unit_prices: 0.05\n    unit: 次', '2025-01-01'),

('ppt_ner',           'pp_ner',           '默认定价', 'unit_values:\n  千字: 1000\nfields:\n  price_factors:\n    type: string\n    role: factor\n    label: 计价因子\n  unit_prices:\n    type: float\n    role: factor\n    label: 单位定价(元)\n  unit:\n    type: string\n    role: factor\n    label: 计价单位\npricings:\n  - price_factors: per_kchars\n    unit_prices: 0.50\n    unit: 千字', '2025-01-01'),

('ppt_songrate',      'pp_songrate',      '默认定价', 'unit_values:\n  次: 1\nfields:\n  price_factors:\n    type: string\n    role: factor\n    label: 计价因子\n  unit_prices:\n    type: float\n    role: factor\n    label: 单位定价(元)\n  unit:\n    type: string\n    role: factor\n    label: 计价单位\npricings:\n  - price_factors: per_call\n    unit_prices: 0.30\n    unit: 次', '2025-01-01'),

('ppt_synth',         'pp_synth',         '默认定价', 'unit_values:\n  秒: 1\nfields:\n  price_factors:\n    type: string\n    role: factor\n    label: 计价因子\n  unit_prices:\n    type: float\n    role: factor\n    label: 单位定价(元)\n  unit:\n    type: string\n    role: factor\n    label: 计价单位\npricings:\n  - price_factors: per_second\n    unit_prices: 0.20\n    unit: 秒', '2025-01-01'),

('ppt_demucs',        'pp_demucs',        '默认定价', 'unit_values:\n  秒: 1\nfields:\n  price_factors:\n    type: string\n    role: factor\n    label: 计价因子\n  unit_prices:\n    type: float\n    role: factor\n    label: 单位定价(元)\n  unit:\n    type: string\n    role: factor\n    label: 计价单位\npricings:\n  - price_factors: per_second\n    unit_prices: 0.02\n    unit: 秒', '2025-01-01'),

('ppt_rvc',           'pp_rvc',           '默认定价', 'unit_values:\n  秒: 1\nfields:\n  price_factors:\n    type: string\n    role: factor\n    label: 计价因子\n  unit_prices:\n    type: float\n    role: factor\n    label: 单位定价(元)\n  unit:\n    type: string\n    role: factor\n    label: 计价单位\npricings:\n  - price_factors: per_second\n    unit_prices: 0.15\n    unit: 秒', '2025-01-01');

-- ============================================================
-- 10. 更新 llm_api_map 绑定 ppid
-- ============================================================
UPDATE llm_api_map SET ppid = 'pp_ktv_pipeline'  WHERE llmid = 'llm_ktv_pipeline';
UPDATE llm_api_map SET ppid = 'pp_video_eval'    WHERE llmid = 'llm_video_eval';
UPDATE llm_api_map SET ppid = 'pp_realesrgan'    WHERE llmid = 'llm_realesrgan';
UPDATE llm_api_map SET ppid = 'pp_asr'           WHERE llmid = 'llm_asr';
UPDATE llm_api_map SET ppid = 'pp_face'          WHERE llmid = 'llm_face';
UPDATE llm_api_map SET ppid = 'pp_graph'         WHERE llmid = 'llm_graph';
UPDATE llm_api_map SET ppid = 'pp_ner'           WHERE llmid = 'llm_ner';
UPDATE llm_api_map SET ppid = 'pp_songrate'      WHERE llmid = 'llm_songrate';
UPDATE llm_api_map SET ppid = 'pp_synth'         WHERE llmid = 'llm_synth';
UPDATE llm_api_map SET ppid = 'pp_demucs'        WHERE llmid = 'llm_demucs';
UPDATE llm_api_map SET ppid = 'pp_rvc'           WHERE llmid = 'llm_rvc';
