package top.continew.admin.business.service.impl;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.transaction.annotation.Transactional;
import top.continew.admin.business.mapper.DigitalHumanMapper;
import top.continew.admin.business.mapper.EffectMapper;
import top.continew.admin.business.mapper.SubtitleItemMapper;
import top.continew.admin.business.mapper.SubtitleStyleMapper;
import top.continew.admin.business.model.entity.DigitalHuman;
import top.continew.admin.business.model.entity.Effect;
import top.continew.admin.business.model.entity.SubtitleItem;
import top.continew.admin.business.model.entity.SubtitleStyle;
import top.continew.admin.business.model.req.DigitalHumanReq;
import top.continew.admin.business.service.DigitalHumanService;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

/**
 * 数字人服务实现类测试
 *
 * @author 王志维
 * @since 2025/12/29
 */
@SpringBootTest
@ActiveProfiles("test")
@Transactional
@Rollback(true)
class DigitalHumanServiceImplTest {

    @Autowired
    private DigitalHumanService digitalHumanService;

    @Autowired
    private DigitalHumanMapper digitalHumanMapper;

    @Autowired
    private SubtitleStyleMapper subtitleStyleMapper;

    @Autowired
    private SubtitleItemMapper subtitleItemMapper;

    @Autowired
    private EffectMapper effectMapper;

    private DigitalHumanReq createDigitalHumanReq() {
        DigitalHumanReq req = new DigitalHumanReq();
        req.setUserId("1");
        req.setCopywriting("测试文案");
        req.setGeneratedVoiceUrl("http://test.com/voice.mp3");
        req.setGeneratedFrameImageUrl("http://test.com/frame.jpg");
        req.setGeneratedVideoUrl("http://test.com/video.mp4");

        // 添加字幕样式
        SubtitleStyle subtitleStyle = new SubtitleStyle();
        subtitleStyle.setFontSize(16);
        subtitleStyle.setColor("#FFFFFF");
        subtitleStyle.setStrokeColor("#000000");
        subtitleStyle.setStrokeWidth(2);
        req.setSubtitleStyle(subtitleStyle);

        // 添加字幕项
        SubtitleItem subtitleItem1 = new SubtitleItem();
        subtitleItem1.setStartTime(0.0);
        subtitleItem1.setEndTime(1.0);
        subtitleItem1.setText("测试字幕1");

        SubtitleItem subtitleItem2 = new SubtitleItem();
        subtitleItem2.setStartTime(1.0);
        subtitleItem2.setEndTime(2.0);
        subtitleItem2.setText("测试字幕2");

        // 添加特效
        Effect effect1 = new Effect();
        effect1.setType("text");
        effect1.setName("测试特效1");
        effect1.setSourceUrl("http://test.com/effect1.mp4");
        effect1.setLength(5.0);

        Effect effect2 = new Effect();
        effect2.setType("image");
        effect2.setName("测试特效2");
        effect2.setSourceUrl("http://test.com/effect2.jpg");
        effect2.setLength(3.0);

        return req;
    }

    @Test
    void testAdd() {
        DigitalHumanReq req = createDigitalHumanReq();
        Long id = digitalHumanService.add(req);

        assertNotNull(id);

        // 验证数字人基本信息
        DigitalHuman digitalHuman = digitalHumanMapper.selectById(id);
        assertNotNull(digitalHuman);
        assertEquals(req.getUserId(), digitalHuman.getUserId());
        assertEquals(req.getCopywriting(), digitalHuman.getCopywriting());

        // 验证字幕样式
        List<SubtitleStyle> subtitleStyles = subtitleStyleMapper.selectList(null);
        assertFalse(subtitleStyles.isEmpty());
        SubtitleStyle savedStyle = subtitleStyles.stream()
            .filter(style -> style.getDigitalHumanId().equals(id))
            .findFirst()
            .orElse(null);
        assertNotNull(savedStyle);
        assertEquals(req.getSubtitleStyle().getFontSize(), savedStyle.getFontSize());
        assertEquals(req.getSubtitleStyle().getColor(), savedStyle.getColor());

        // 验证字幕项
        List<SubtitleItem> subtitleItems = subtitleItemMapper.selectList(null);
        assertFalse(subtitleItems.isEmpty());
        long subtitleCount = subtitleItems.stream().filter(item -> item.getDigitalHumanId().equals(id)).count();
        assertEquals(2, subtitleCount);

        // 验证特效
        List<Effect> effects = effectMapper.selectList(null);
        assertFalse(effects.isEmpty());
        long effectCount = effects.stream().filter(effect -> effect.getDigitalHumanId().equals(id)).count();
        assertEquals(2, effectCount);
    }

    @Test
    void testUpdate() {
        // 先添加一个数字人
        DigitalHumanReq addReq = createDigitalHumanReq();
        Long id = digitalHumanService.add(addReq);
        assertNotNull(id);

        // 准备更新数据
        DigitalHumanReq updateReq = new DigitalHumanReq();
        updateReq.setUserId("1");
        updateReq.setCopywriting("更新后的文案");
        updateReq.setGeneratedVoiceUrl("http://test.com/voice-updated.mp3");
        updateReq.setGeneratedFrameImageUrl("http://test.com/frame-updated.jpg");
        updateReq.setGeneratedVideoUrl("http://test.com/video-updated.mp4");

        // 更新字幕样式
        SubtitleStyle updatedStyle = new SubtitleStyle();
        updatedStyle.setFontSize(20);
        updatedStyle.setColor("#FF0000");
        updatedStyle.setStrokeColor("#0000FF");
        updatedStyle.setStrokeWidth(3);
        updateReq.setSubtitleStyle(updatedStyle);

        // 执行更新
        digitalHumanService.update(updateReq, id);

        // 验证数字人基本信息更新
        DigitalHuman updatedDigitalHuman = digitalHumanMapper.selectById(id);
        assertNotNull(updatedDigitalHuman);
        assertEquals(updateReq.getCopywriting(), updatedDigitalHuman.getCopywriting());
        assertEquals(updateReq.getGeneratedVoiceUrl(), updatedDigitalHuman.getGeneratedVoiceUrl());

        // 验证字幕样式更新
        List<SubtitleStyle> subtitleStyles = subtitleStyleMapper.selectList(null);
        SubtitleStyle savedStyle = subtitleStyles.stream()
            .filter(style -> style.getDigitalHumanId().equals(id))
            .findFirst()
            .orElse(null);
        assertNotNull(savedStyle);
        assertEquals(updatedStyle.getFontSize(), savedStyle.getFontSize());
        assertEquals(updatedStyle.getColor(), savedStyle.getColor());
        assertEquals(updatedStyle.getStrokeColor(), savedStyle.getStrokeColor());

        // 验证字幕项更新（数量应为1）
        List<SubtitleItem> subtitleItems = subtitleItemMapper.selectList(null);
        long subtitleCount = subtitleItems.stream().filter(item -> item.getDigitalHumanId().equals(id)).count();
        assertEquals(1, subtitleCount);

        // 验证特效更新（数量应为1）
        List<Effect> effects = effectMapper.selectList(null);
        long effectCount = effects.stream().filter(effect -> effect.getDigitalHumanId().equals(id)).count();
        assertEquals(1, effectCount);
    }

    @Test
    void testDelete() {
        // 先添加一个数字人
        DigitalHumanReq req = createDigitalHumanReq();
        Long id = digitalHumanService.add(req);
        assertNotNull(id);

        // 验证添加成功
        assertNotNull(digitalHumanMapper.selectById(id));

        // 执行删除
        digitalHumanService.delete(List.of(id));

        // 验证数字人已删除
        assertNull(digitalHumanMapper.selectById(id));

        // 验证关联的字幕样式已删除
        List<SubtitleStyle> subtitleStyles = subtitleStyleMapper.selectList(null);
        long styleCount = subtitleStyles.stream().filter(style -> style.getDigitalHumanId().equals(id)).count();
        assertEquals(0, styleCount);

        // 验证关联的字幕项已删除
        List<SubtitleItem> subtitleItems = subtitleItemMapper.selectList(null);
        long subtitleCount = subtitleItems.stream().filter(item -> item.getDigitalHumanId().equals(id)).count();
        assertEquals(0, subtitleCount);

        // 验证关联的特效已删除
        List<Effect> effects = effectMapper.selectList(null);
        long effectCount = effects.stream().filter(effect -> effect.getDigitalHumanId().equals(id)).count();
        assertEquals(0, effectCount);
    }
}
