-- ============================================================
-- KTV服务 uapi 补全 Part 2
-- data使用_file命名; response用{%if error%}容错透传GPU响应
-- async: synth-generate(5s轮询), realesrgan-upscale(3s轮询)
-- ============================================================

-- 1. ASR语音识别 (sync)
UPDATE uapi SET stream='sync',headers='{"Content-Type":"application/json"}',
  data='{"audio_file":"{{audio_file}}"{%if language%},"language":"{{language}}"{%endif%}}',
  response='{%if error%}{"error":"{{error}}"}{%else%}{"status":"{{status}}","text":"{{text}}","segments":{{json.dumps(segments,ensure_ascii=False)}},"usage":{{json.dumps(usage,ensure_ascii=False)}}}{%endif%}',
  ioid='ktv_asr_transcribe_io' WHERE id='uapi_ktv_asr_transcribe';

-- 2. Demucs音频分离 (async)
UPDATE uapi SET stream='async',headers='{"Content-Type":"application/json"}',
  data='{"audio_path":"{{audio_file}}"{%if task_type%},"task_type":"{{task_type}}"{%endif%}}',
  response='{"taskid":"{{task_id}}","taskstatus":"PENDING","status":"PENDING"}',
  ioid='ktv_demucs_separate_io' WHERE id='uapi_ktv_demucs_separate';

-- 3. 人脸检测 (sync)
UPDATE uapi SET stream='sync',headers='{"Content-Type":"application/json"}',
  data='{"image_file":"{{image_file}}"}',
  response='{%if error%}{"error":"{{error}}"}{%else%}{"status":"{{status}}","faces":{{json.dumps(faces,ensure_ascii=False)}},"usage":{{json.dumps(usage,ensure_ascii=False)}}}{%endif%}',
  ioid='ktv_face_detect_io' WHERE id='uapi_ktv_face_detect';

-- 4. 人脸识别 (sync)
UPDATE uapi SET stream='sync',headers='{"Content-Type":"application/json"}',
  data='{"image_file":"{{image_file}}"{%if face_id%},"face_id":"{{face_id}}"{%endif%}}',
  response='{%if error%}{"error":"{{error}}"}{%else%}{"status":"{{status}}","matches":{{json.dumps(matches,ensure_ascii=False)}},"usage":{{json.dumps(usage,ensure_ascii=False)}}}{%endif%}',
  ioid='ktv_face_recognize_io' WHERE id='uapi_ktv_face_recognize';

-- 5. 人脸比对 (sync)
UPDATE uapi SET stream='sync',headers='{"Content-Type":"application/json"}',
  data='{"image_file1":"{{image_file1}}","image_file2":"{{image_file2}}"}',
  response='{%if error%}{"error":"{{error}}"}{%else%}{"status":"{{status}}","similarity":{{similarity}},"usage":{{json.dumps(usage,ensure_ascii=False)}}}{%endif%}',
  ioid='ktv_face_compare_io' WHERE id='uapi_ktv_face_compare';

-- 6. 字幕渲染 (sync)
UPDATE uapi SET stream='sync',headers='{"Content-Type":"application/json"}',
  data='{"lyrics_json":{{lyrics_json}}{%if width%},"width":{{width}}{%endif%}{%if height%},"height":{{height}}{%endif%}}',
  response='{%if error%}{"error":"{{error}}"}{%else%}{"status":"{{status}}","ass_url":"{{ass_url}}","usage":{{json.dumps(usage,ensure_ascii=False)}}}{%endif%}',
  ioid='ktv_subtitle_render_io' WHERE id='uapi_ktv_subtitle_render';

-- 7. 视频合并 (sync)
UPDATE uapi SET stream='sync',headers='{"Content-Type":"application/json"}',
  data='{%set c=joiner(",")%}{{%if video_file%}{{c()}}"video_file":"{{video_file}}"{%endif%}{%if audio_file%}{{c()}}"audio_file":"{{audio_file}}"{%endif%}{%if subtitle_file%}{{c()}}"subtitle_file":"{{subtitle_file}}"{%endif%}{%if output_format%}{{c()}}"output_format":"{{output_format}}"{%endif%}}',
  response='{%if error%}{"error":"{{error}}"}{%else%}{"status":"{{status}}","output_url":"{{output_url}}","duration":{{duration}},"usage":{{json.dumps(usage,ensure_ascii=False)}}}{%endif%}',
  ioid='ktv_merge_video_io' WHERE id='uapi_ktv_merge_video';

-- 8. 歌曲评分 (sync)
UPDATE uapi SET stream='sync',headers='{"Content-Type":"application/json"}',
  data='{"audio_file":"{{audio_file}}"{%if scene%},"scene":"{{scene}}"{%endif%}}',
  response='{%if error%}{"error":"{{error}}"}{%else%}{"status":"{{status}}","score":{{score}},"details":{{json.dumps(details,ensure_ascii=False)}},"usage":{{json.dumps(usage,ensure_ascii=False)}}}{%endif%}',
  ioid='ktv_songrate_evaluate_io' WHERE id='uapi_ktv_songrate_evaluate';

-- 9. 合成状态查询 (sync)
UPDATE uapi SET stream='sync',headers='{"Content-Type":"application/json"}',
  data='{"task_id":"{{task_id}}"}',
  response='{%if error%}{"error":"{{error}}"}{%elif status=="SUCCEEDED"%}{"taskstatus":"SUCCEEDED","output_url":"{{output_url}}","usage":{{json.dumps(usage,ensure_ascii=False)}}}{%elif status=="FAILED"%}{"taskstatus":"FAILED","error":"{{error}}"}{%else%}{"taskstatus":"PENDING"}{%endif%}',
  ioid='ktv_synth_status_io' WHERE id='uapi_ktv_synth_status';

-- 11. 超分辨率 (async提交)
UPDATE uapi SET stream='async',headers='{"Content-Type":"application/json"}',
  data='{"image_file":"{{image_file}}"{%if scale%},"scale":{{scale}}{%endif%}}',
  response='{"taskid":"{{task_id}}","taskstatus":"PENDING"}',
  ioid='ktv_realesrgan_upscale_io' WHERE id='uapi_ktv_realesrgan_upscale';

-- 12. 超分状态查询 (sync)
UPDATE uapi SET stream='sync',headers='{"Content-Type":"application/json"}',
  data=NULL,
  response='{"status":"{{status}}","output_url":"{{output_url}}","usage":{{json.dumps(usage,ensure_ascii=False)}}}',
  ioid='ktv_realesrgan_status_io' WHERE id='uapi_ktv_realesrgan_status';

-- 13. RVC声音转换 (sync)
UPDATE uapi SET stream='sync',headers='{"Content-Type":"application/json"}',
  data='{"audio_file":"{{audio_file}}","voice_model":"{{voice_model}}"}',
  response='{%if error%}{"error":"{{error}}"}{%else%}{"status":"{{status}}","output_url":"{{output_url}}","usage":{{json.dumps(usage,ensure_ascii=False)}}}{%endif%}',
  ioid='ktv_rvc_convert_io' WHERE id='uapi_ktv_rvc_convert';

-- 14. 视频评估 (sync)
UPDATE uapi SET stream='sync',headers='{"Content-Type":"application/json"}',
  data='{"video_file":"{{video_file}}"}',
  response='{%if error%}{"error":"{{error}}"}{%else%}{"status":"{{status}}","score":{{score}},"details":{{json.dumps(details,ensure_ascii=False)}},"usage":{{json.dumps(usage,ensure_ascii=False)}}}{%endif%}',
  ioid='ktv_video_eval_evaluate_io' WHERE id='uapi_ktv_video_eval_evaluate';

-- async query_apiname
UPDATE llm_api_map SET query_apiname='realesrgan-status',query_period=5 WHERE llmid='llm_ktv_realesrgan_upscale';

-- ppid (v1/models 过滤要求 ppid IS NOT NULL)
UPDATE llm_api_map SET ppid='pp_ktv' WHERE llmid LIKE 'llm_ktv%' AND ppid IS NULL;

-- upappkey (API调用鉴权必需)
INSERT INTO upappkey (id,upappid,ownerid,apikey,orgid)
SELECT 'ktv_gateway_key','ktv-gateway','UiEi7hKqAmU1-jqQEVhZe','','0'
WHERE NOT EXISTS (SELECT 1 FROM upappkey WHERE upappid='ktv-gateway');
