-- ============================================================
-- KTV服务 uapi 补全迁移脚本 Part 2
-- 更新14条uapi记录：stream/data/response/ioid/headers
-- ============================================================

-- ============================================================
-- 1. ASR语音识别 (sync)
--    GPU: /asr/api/asr-transcribe → localhost:9925
--    uapiio: ktv_asr_transcribe_io
-- ============================================================
UPDATE uapi SET
  stream = 'sync',
  headers = '{"Content-Type": "application/json"}',
  data = '{
    "audio_url": "{{audio_url}}"
{% if language %}
    ,"language": "{{language}}"
{% endif %}
}',
  response = '{
    "status": "{{status}}",
    "text": "{{text}}",
    "segments": {{json.dumps(segments, ensure_ascii=False)}},
    "usage": {"audio_seconds": {{audio_seconds|float}}}
}',
  ioid = 'ktv_asr_transcribe_io'
WHERE id = 'uapi_ktv_asr_transcribe';

-- ============================================================
-- 2. Demucs音频分离 (sync)
--    GPU: /demucs/api/demucs-separate → localhost:9083
-- ============================================================
UPDATE uapi SET
  stream = 'sync',
  headers = '{"Content-Type": "application/json"}',
  data = '{
    "audio_url": "{{audio_url}}"
}',
  response = '{
    "status": "{{status}}",
    "vocals_url": "{{vocals_url}}",
    "accompaniment_url": "{{accompaniment_url}}",
    "usage": {"audio_seconds": {{audio_seconds|float}}}
}',
  ioid = 'ktv_demucs_separate_io'
WHERE id = 'uapi_ktv_demucs_separate';

-- ============================================================
-- 3. 人脸检测 (sync)
--    GPU: /face/api/face-detect → localhost:9091
-- ============================================================
UPDATE uapi SET
  stream = 'sync',
  headers = '{"Content-Type": "application/json"}',
  data = '{
    "image_url": "{{image_url}}"
}',
  response = '{
    "status": "{{status}}",
    "faces": {{json.dumps(faces, ensure_ascii=False)}},
    "usage": {"detect_count": {{faces|length}}}
}',
  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_url": "{{image_url}}"
{% if face_id %}
    ,"face_id": "{{face_id}}"
{% endif %}
}',
  response = '{
    "status": "{{status}}",
    "matches": {{json.dumps(matches, ensure_ascii=False)}},
    "usage": {"recognize_count": {{matches|length}}}
}',
  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_url1": "{{image_url1}}",
    "image_url2": "{{image_url2}}"
}',
  response = '{
    "status": "{{status}}",
    "similarity": {{similarity|float}},
    "usage": {"compare_count": 1}
}',
  ioid = 'ktv_face_compare_io'
WHERE id = 'uapi_ktv_face_compare';

-- ============================================================
-- 6. 字幕渲染 (sync)
--    GPU: /subtitle/api/subtitle-render → localhost:9080
-- ============================================================
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 = '{
    "status": "{{status}}",
    "ass_url": "{{ass_url}}",
    "usage": {"subtitle_count": {{segments|length}}}
}',
  ioid = 'ktv_subtitle_render_io'
WHERE id = 'uapi_ktv_subtitle_render';

-- ============================================================
-- 7. 视频合并 (sync)
--    GPU: /merge/api/merge-video → localhost:9080
-- ============================================================
UPDATE uapi SET
  stream = 'sync',
  headers = '{"Content-Type": "application/json"}',
  data = '{
{% if video_url %}
    "video_url": "{{video_url}}"
{% endif %}
{% if audio_url %}
    ,"audio_url": "{{audio_url}}"
{% endif %}
{% if subtitle_url %}
    ,"subtitle_url": "{{subtitle_url}}"
{% endif %}
{% if output_format %}
    ,"output_format": "{{output_format}}"
{% endif %}
}',
  response = '{
    "status": "{{status}}",
    "output_url": "{{output_url}}",
    "duration": {{duration|float}},
    "usage": {"output_seconds": {{duration|float}}}
}',
  ioid = 'ktv_merge_video_io'
WHERE id = 'uapi_ktv_merge_video';

-- ============================================================
-- 8. 歌曲评分 (sync)
--    GPU: /songrate/api/songrate-evaluate → localhost:8901
-- ============================================================
UPDATE uapi SET
  stream = 'sync',
  headers = '{"Content-Type": "application/json"}',
  data = '{
    "audio_url": "{{audio_url}}"
{% if scene %}
    ,"scene": "{{scene}}"
{% endif %}
}',
  response = '{
    "status": "{{status}}",
    "score": {{score|float}},
    "details": {{json.dumps(details, ensure_ascii=False)}},
    "usage": {"audio_seconds": {{audio_seconds|float}}}
}',
  ioid = 'ktv_songrate_evaluate_io'
WHERE id = 'uapi_ktv_songrate_evaluate';

-- ============================================================
-- 9. 音乐合成 (async - 提交任务)
--    GPU: /synth/api/synth-generate → localhost:9084
-- ============================================================
UPDATE uapi SET
  stream = 'async',
  headers = '{"Content-Type": "application/json"}',
  data = '{
    "lyrics": "{{lyrics}}"
{% if style %}
    ,"style": "{{style}}"
{% endif %}
{% if reference_audio %}
    ,"reference_audio": "{{reference_audio}}"
{% endif %}
}',
  response = '{
    "taskid": "{{task_id}}",
    "taskstatus": "PENDING"
}',
  ioid = 'ktv_synth_generate_io'
WHERE id = 'uapi_ktv_synth_generate';

-- ============================================================
-- 10. 合成状态查询 (sync)
--    GPU: /synth/api/synth-status → localhost:9084
-- ============================================================
UPDATE uapi SET
  stream = 'sync',
  headers = '{"Content-Type": "application/json"}',
  data = '{"task_id": "{{task_id}}"}',
  response = '{
{% if status == "SUCCEEDED" %}
    "taskstatus": "SUCCEEDED",
    "output_url": "{{output_url}}",
    "usage": {"audio_seconds": {{duration|float}}}
{% elif status == "FAILED" %}
    "taskstatus": "FAILED",
    "error": "{{error}}"
{% else %}
    "taskstatus": "PENDING"
{% endif %}
}',
  ioid = 'ktv_synth_status_io'
WHERE id = 'uapi_ktv_synth_status';

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

-- ============================================================
-- 12. 超分状态查询 (sync)
--    GPU: /realesrgan/api/realesrgan-status → localhost:9082
-- ============================================================
UPDATE uapi SET
  stream = 'sync',
  headers = '{"Content-Type": "application/json"}',
  data = '{"task_id": "{{task_id}}"}',
  response = '{
{% if status == "SUCCEEDED" %}
    "taskstatus": "SUCCEEDED",
    "output_url": "{{output_url}}",
    "usage": {"image_count": 1}
{% elif status == "FAILED" %}
    "taskstatus": "FAILED",
    "error": "{{error}}"
{% else %}
    "taskstatus": "PENDING"
{% endif %}
}',
  ioid = 'ktv_realesrgan_status_io'
WHERE id = 'uapi_ktv_realesrgan_status';

-- ============================================================
-- 13. RVC声音转换 (sync)
--    GPU: /rvc/api/rvc-convert → localhost:9085
-- ============================================================
UPDATE uapi SET
  stream = 'sync',
  headers = '{"Content-Type": "application/json"}',
  data = '{
    "audio_url": "{{audio_url}}",
    "voice_model": "{{voice_model}}"
}',
  response = '{
    "status": "{{status}}",
    "output_url": "{{output_url}}",
    "usage": {"audio_seconds": {{audio_seconds|float}}}
}',
  ioid = 'ktv_rvc_convert_io'
WHERE id = 'uapi_ktv_rvc_convert';

-- ============================================================
-- 14. 视频评估 (sync)
--    GPU: /video-eval/api/video-eval-evaluate → localhost:8902
-- ============================================================
UPDATE uapi SET
  stream = 'sync',
  headers = '{"Content-Type": "application/json"}',
  data = '{
    "video_url": "{{video_url}}"
}',
  response = '{
    "status": "{{status}}",
    "score": {{score|float}},
    "details": {{json.dumps(details, ensure_ascii=False)}},
    "usage": {"video_seconds": {{video_seconds|float}}}
}',
  ioid = 'ktv_video_eval_evaluate_io'
WHERE id = 'uapi_ktv_video_eval_evaluate';

-- ============================================================
-- 关键：更新 llm_api_map 中的 async 模型的 query_apiname
-- synth-generate 查询 synth-status
-- realesrgan-upscale 查询 realesrgan-status
-- ============================================================
UPDATE llm_api_map SET query_apiname = 'synth-status', query_period = 5
WHERE llmid = 'llm_ktv_synth_generate' AND llmcatelogid = 'ktv_pipeline';

UPDATE llm_api_map SET query_apiname = 'realesrgan-status', query_period = 3
WHERE llmid = 'llm_ktv_realesrgan_upscale' AND llmcatelogid = 'ktv_pipeline';
