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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import top.continew.admin.business.mapper.SubtitleStyleMapper;
import top.continew.admin.business.model.entity.SubtitleStyle;
import top.continew.admin.business.model.query.SubtitleStylePageQuery;
import top.continew.admin.business.model.query.SubtitleStyleQuery;
import top.continew.admin.business.model.req.SubtitleStyleReq;
import top.continew.admin.business.model.resp.SubtitleStyleDetailResp;
import top.continew.admin.business.model.resp.SubtitleStyleResp;
import top.continew.admin.business.service.SubtitleStyleService;
import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.extension.crud.service.impl.BaseServiceImpl;

/**
 * 数字人-字幕样式服务实现类
 *
 * @author 王志维
 * @since 2025/12/29
 */
@Service
@RequiredArgsConstructor
public class SubtitleStyleServiceImpl extends BaseServiceImpl<SubtitleStyleMapper, SubtitleStyle, SubtitleStyleResp, SubtitleStyleDetailResp, SubtitleStyleQuery, SubtitleStyleReq> implements SubtitleStyleService {

    private final SubtitleStyleMapper subtitleStyleMapper;

    @Override
    public PageResp<SubtitleStyleResp> pageList(SubtitleStyleQuery query, SubtitleStylePageQuery pageQuery) {
        LambdaQueryWrapper<SubtitleStyle> wrapper = new LambdaQueryWrapper<>();
        if (query.getDigitalHumanId() != null) {
            wrapper.eq(SubtitleStyle::getDigitalHumanId, query.getDigitalHumanId());
        }
        if (query.getFontSize() != null) {
            wrapper.eq(SubtitleStyle::getFontSize, query.getFontSize());
        }
        if (query.getFontFamily() != null) {
            wrapper.eq(SubtitleStyle::getFontFamily, query.getFontFamily());
        }
        if (query.getColor() != null) {
            wrapper.eq(SubtitleStyle::getColor, query.getColor());
        }

        wrapper.orderByDesc(SubtitleStyle::getCreateTime);

        Page<SubtitleStyle> page = new Page<>(pageQuery.getPage(), pageQuery.getSize());

        Page<SubtitleStyle> result = subtitleStyleMapper.selectPage(page, wrapper);
        PageResp<SubtitleStyleResp> pageResp = new PageResp();
        pageResp.setTotal(result.getTotal());
        pageResp.setList(result.getRecords().stream().map(this::convertToResp).toList());
        return pageResp;
    }

    @Override
    public Long add(SubtitleStyleReq req) {
        SubtitleStyle subtitleStyle = new SubtitleStyle();
        BeanUtils.copyProperties(req, subtitleStyle);
        subtitleStyle.setCreateTime(java.time.LocalDateTime.now());
        subtitleStyle.setUpdateTime(java.time.LocalDateTime.now());
        subtitleStyleMapper.insert(subtitleStyle);
        return subtitleStyle.getId();
    }

    @Override
    public void update(SubtitleStyleReq req, Long id) {
        SubtitleStyle subtitleStyle = new SubtitleStyle();
        BeanUtils.copyProperties(req, subtitleStyle);
        subtitleStyle.setId(id);
        subtitleStyle.setUpdateTime(java.time.LocalDateTime.now());
        subtitleStyleMapper.updateById(subtitleStyle);
    }

    private SubtitleStyleResp convertToResp(SubtitleStyle subtitleStyle) {
        SubtitleStyleResp resp = new SubtitleStyleResp();
        BeanUtils.copyProperties(subtitleStyle, resp);
        return resp;
    }
}