package com.rj.service;

import com.rj.dto.SiliconFlowTtsRequest;
import com.rj.dto.SiliconFlowTtsResponse;
import com.rj.entity.TtsRequestLog;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.LocalDateTime;

/**
 * TTS请求日志音频名称字段测试
 * 
 * @author rj
 * @date 2025-01-02
 */
@Slf4j
@SpringBootTest
public class TtsRequestLogWithAudioNameTest {
    
    @Autowired
    private ITtsRequestLogService ttsRequestLogService;
    
    @Autowired
    private SiliconFlowTtsService siliconFlowTtsService;
    
    /**
     * 测试TTS请求并验证音频名称字段
     */
    @Test
    public void testTtsRequestWithAudioName() {
        try {
            // 创建TTS请求
            SiliconFlowTtsRequest request = new SiliconFlowTtsRequest();
            request.setInput("这是一个测试音频文件，用于验证音频名称字段功能。");
            request.setModel("fnlp/MOSS-TTSD-v0.5");
            request.setVoice("fnlp/MOSS-TTSD-v0.5:claire");
            request.setCreatorName("测试用户");
            request.setCreatorPhone("13800138000");
            
            log.info("开始TTS请求测试...");
            log.info("请求参数: 模型={}, 语音={}, 输入文本={}", 
                request.getModel(), request.getVoice(), request.getInput());
            
            // 执行TTS请求
            SiliconFlowTtsResponse response = siliconFlowTtsService.textToSpeech(request);
            
            if (response.isSuccess()) {
                log.info("TTS请求成功！");
                log.info("响应信息: 格式={}, 采样率={}, 时长={}秒", 
                    response.getFormat(), response.getSampleRate(), response.getDuration());
                log.info("MinIO URL: {}", response.getMinioUrl());
                log.info("临时URL: {}", response.getShortUrl());
                
                // 验证音频名称字段是否正确保存
                // 注意：这里需要根据实际的日志ID来查询，实际使用时可能需要调整
                log.info("请检查数据库中的 tts_request_log 表，确认 audio_name 字段是否正确保存");
                
            } else {
                log.error("TTS请求失败: {}", response.getMessage());
            }
            
        } catch (Exception e) {
            log.error("测试过程中发生异常: {}", e.getMessage(), e);
        }
    }
    
    /**
     * 测试手动创建TTS请求日志并设置音频名称
     */
    @Test
    public void testManualTtsRequestLogWithAudioName() {
        try {
            // 创建测试日志
            TtsRequestLog requestLog = new TtsRequestLog();
            requestLog.setRequestTime(LocalDateTime.now());
            requestLog.setModel("fnlp/MOSS-TTSD-v0.5");
            requestLog.setInputText("测试音频文件");
            requestLog.setInputLength(6);
            requestLog.setVoice("fnlp/MOSS-TTSD-v0.5:claire");
            requestLog.setStatus("SUCCESS");
            requestLog.setAudioName("test_audio_20250102_120000_12345.mp3");
            requestLog.setMinioUrl("http://localhost:9000/audio/test_audio_20250102_120000_12345.mp3");
            requestLog.setCreatorName("测试用户");
            requestLog.setCreatorPhone("13800138000");
            requestLog.setCreateTime(LocalDateTime.now());
            requestLog.setUpdateTime(LocalDateTime.now());
            
            // 保存到数据库
            boolean result = ttsRequestLogService.saveTtsRequestLog(requestLog);
            
            if (result) {
                log.info("TTS请求日志保存成功，ID: {}", requestLog.getId());
                log.info("音频名称: {}", requestLog.getAudioName());
                
                // 查询验证
                TtsRequestLog savedLog = ttsRequestLogService.getTtsRequestLogById(requestLog.getId());
                if (savedLog != null) {
                    log.info("查询验证成功，音频名称: {}", savedLog.getAudioName());
                } else {
                    log.error("查询验证失败，未找到保存的日志");
                }
                
            } else {
                log.error("TTS请求日志保存失败");
            }
            
        } catch (Exception e) {
            log.error("手动测试过程中发生异常: {}", e.getMessage(), e);
        }
    }
}
