package cn.iocoder.byzs.module.ai.framework.ai.core.model.music;

import cn.hutool.core.collection.ListUtil;
import cn.iocoder.byzs.module.ai.framework.ai.core.model.suno.api.SunoApi;
import com.fasterxml.jackson.core.JsonProcessingException;
import kong.unirest.HttpResponse;
import kong.unirest.RequestBodyEntity;
import kong.unirest.Unirest;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.util.List;

/**
 * {@link SunoApi} 集成测试
 *
 * @author xiaoxin
 */
public class SunoApiTests {

    private final SunoApi sunoApi = new SunoApi("https://suno-3tah0ycyt-status2xxs-projects.vercel.app");
//    private final SunoApi sunoApi = new SunoApi("http://127.0.0.1:3001");


    @Test // cs
    public static void main(String[] args) throws JsonProcessingException {
//        Unirest.setTimeouts(0, 0);
//        HttpResponse<String> response = Unirest.post("https://apibox.erweima.ai/api/v1/generate")
//                .header("Content-Type", "application/json")
//                .header("Accept", "application/json")
//                .header("Authorization", "Bearer 1723a5683447c298cf6ea1362c084977")
//                .body("{\n  \"prompt\": \"一段平静舒缓的钢琴曲，带有柔和的旋律\",\n  \"style\": \"古典\",\n  \"title\": \"宁静钢琴冥想\",\n  \"customMode\": true,\n  \"instrumental\": true,\n  \"model\": \"V3_5\",\n  \"negativeTags\": \"重金属, 强节奏鼓点\",\n  \"callBackUrl\": \"https://api.example.com/callback\"\n}")
//                .asString();
//        System.out.println(response.getBody());
//        Unirest.setTimeouts(0, 0);



//        HttpResponse<String> response = Unirest.get("https://apibox.erweima.ai/api/v1/generate/record-info")
//                .header("Accept", "application/json")
//                .header("Authorization", "Bearer 1723a5683447c298cf6ea1362c084977")
//                .asString();

        Unirest.config().connectTimeout(0).socketTimeout(0);  // 连接超时和套接字超时均设为 0（无限制）
        HttpResponse<String> response = Unirest.post("https://apibox.erweima.ai/api/v1/generate")
                .header("Content-Type", "application/json")
                .header("Accept", "application/json")
                .header("Authorization", "Bearer 1723a5683447c298cf6ea1362c084977")
                .body("{\n  \"prompt\": \"一段平静舒缓的钢琴曲，带有柔和的旋律\",\n  \"style\": \"古典\",\n  \"title\": \"宁静钢琴冥想\",\n  \"customMode\": true,\n  \"instrumental\": true,\n  \"model\": \"V3_5\",\n  \"negativeTags\": \"重金属, 强节奏鼓点\",\n  \"callBackUrl\": \"https://api.example.com/callback\"\n}")
                .asString();

        String body = response.getBody();
        // 使用 Jackson 反序列化
        com.fasterxml.jackson.databind.ObjectMapper objectMapper = new com.fasterxml.jackson.databind.ObjectMapper();
        SunoApi.ResMusicData apiResponse = objectMapper.readValue(body, SunoApi.ResMusicData.class);

        // 判断 code 是否为 200
        if (apiResponse.getCode() == 200) {
            String taskId = apiResponse.getData().getTaskId();
            System.out.println("请求成功，taskId：" + taskId);
        } else {
            System.out.println("请求失败，code：" + apiResponse.getCode() + "，message：" + apiResponse.getMsg());
        }
    }

    @Test // 描述模式
    @Disabled
    public void testGenerate() {
        // 准备参数
        SunoApi.MusicGenerateRequest generateRequest = new SunoApi.MusicGenerateRequest(
                "happy music",
                "chirp-v3-5",
                false);

        // 调用方法
        List<SunoApi.MusicData> musicList = sunoApi.generate(generateRequest);
        // 打印结果
        System.out.println(musicList);
    }

    @Test // 歌词模式
    @Disabled
    public void testCustomGenerate() {
        // 准备参数
        SunoApi.MusicGenerateRequest generateRequest = new SunoApi.MusicGenerateRequest(
                "创作一首带有轻松吉他旋律的流行歌曲，[verse] 描述夏日海滩的宁静，[chorus] 节奏加快，表达对自由的向往。",
                "Happy",
                "Happy Song",
                "chirp-v3.5",
                false,
                false, false, null, null);

        // 调用方法
        List<SunoApi.MusicData> musicList = sunoApi.customGenerate(generateRequest);
        // 打印结果
        System.out.println(musicList);
    }

    @Test
    @Disabled
    public void testGenerateLyrics() {
        // 调用方法
        SunoApi.LyricsData lyricsData = sunoApi.generateLyrics("A soothing lullaby");
        // 打印结果
        System.out.println(lyricsData);
    }

    @Test
    @Disabled
    public void testGetMusicList() {
        // 准备参数
//        String id = "d460ddda-7c87-4f34-b751-419b08a590ca";
        String id = "584729e5-0fe9-4157-86da-1b4803ff42bf";

        // 调用方法
        List<SunoApi.MusicData> musicList = sunoApi.getMusicList(ListUtil.of(id));
        // 打印结果
        System.out.println(musicList);
    }

    @Test
    @Disabled
    public void testGetLimitUsage() {
        // 调用方法
        SunoApi.LimitUsageData limitUsageData = sunoApi.getLimitUsage();
        // 打印结果
        System.out.println(limitUsageData);
    }

}
