package com.sonic.cow.client.impl;

import com.alibaba.fastjson.JSONObject;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.sonic.cow.client.AsrClient;
import com.sonic.cow.client.config.AsrCnConfig;
import com.sonic.cow.client.input.asr.AsrInput;
import com.sonic.cow.domain.input.VoiceAsrInput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Slf4j
@Service
public class AsrClientImpl implements AsrClient {

    @Autowired
    private AsrCnConfig asrCnConfig;

    @Override
    public String asr(String userId, VoiceAsrInput input) {
        AsrInput asrInput = new AsrInput(userId, input.getData());
        try {
            //RPC 语音转文本
            HttpResponse<String> result = Unirest.post(asrCnConfig.getFlashUrl())
                    .header("content-type", "application/json")
                    .header("X-Api-App-Key", asrCnConfig.getAsrAppId())
                    .header("X-Api-Access-Key", asrCnConfig.getAsrAccessToken())
                    .header("X-Api-Resource-Id", asrCnConfig.getResourceId())
                    .header("X-Api-Request-Id", UUID.randomUUID().toString())
                    .header("X-Api-Sequence", "-1")
                    .body(JSONObject.toJSONString(asrInput))
                    .asString();
            String body = result.getBody();
            log.info("===> asr body : {}",  body);
            JSONObject jsonObject = JSONObject.parseObject(body);
            return jsonObject.getJSONObject("result").getString("text");
        } catch (UnirestException e) {
            log.error("===> asr error : ", e);
        }
        return "";
    }

    public static void main(String[] args) {
        try {
            // POST 请求到 OpenAI Whisper 转录端点
            HttpResponse<JsonNode> response = Unirest.post("https://api.openai.com/v1/audio/transcriptions")
                    .header("Authorization", "Bearer " + "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")
                    .header("Content-Type", "multipart/form-data")
                    .field("model", "gpt-4o-mini-transcribe")
                    .field("file", "https://sound-oss.epal.gg/data/sound/546963/17005091327548797.mp3")  // 上传音频文件
                    .asJson();

            // 检查响应状态
            if (response.getStatus() == 200) {
                JsonNode body = response.getBody();
                System.out.println("转录结果: " + body.getObject().get("text").toString());
            } else {
                System.err.println("错误: " + response.getStatus() + " - " + response.getBody());
            }
        } catch (UnirestException e) {
            e.printStackTrace();
        }
    }
}
