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.SubtitleItemMapper;
import top.continew.admin.business.model.entity.SubtitleItem;
import top.continew.admin.business.model.query.SubtitleItemPageQuery;
import top.continew.admin.business.model.query.SubtitleItemQuery;
import top.continew.admin.business.model.req.SubtitleItemReq;
import top.continew.admin.business.model.resp.SubtitleItemDetailResp;
import top.continew.admin.business.model.resp.SubtitleItemResp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import top.continew.admin.business.service.SubtitleItemService;
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 SubtitleItemServiceImpl extends BaseServiceImpl<SubtitleItemMapper, SubtitleItem, SubtitleItemResp, SubtitleItemDetailResp, SubtitleItemQuery, SubtitleItemReq> implements SubtitleItemService {

    private final SubtitleItemMapper subtitleItemMapper;

    @Override
    public PageResp<SubtitleItemResp> pageList(SubtitleItemQuery query, SubtitleItemPageQuery pageQuery) {
        LambdaQueryWrapper<SubtitleItem> wrapper = new LambdaQueryWrapper<>();
        if (query.getDigitalHumanId() != null) {
            wrapper.eq(SubtitleItem::getDigitalHumanId, query.getDigitalHumanId());
        }
        if (query.getText() != null) {
            wrapper.like(SubtitleItem::getText, query.getText());
        }
        if (query.getStartTime() != null) {
            wrapper.ge(SubtitleItem::getStartTime, query.getStartTime());
        }
        if (query.getEndTime() != null) {
            wrapper.le(SubtitleItem::getEndTime, query.getEndTime());
        }

        wrapper.orderByDesc(SubtitleItem::getCreateTime);

        Page<SubtitleItem> page = new Page<>(pageQuery.getPage(), pageQuery.getSize());
        Page<SubtitleItem> result = subtitleItemMapper.selectPage(page, wrapper);

        PageResp<SubtitleItemResp> pageResp = new PageResp();
        pageResp.setTotal(result.getTotal());
        pageResp.setList(result.getRecords().stream().map(this::convertToResp).toList());
        return pageResp;
    }

    @Override
    public Long add(SubtitleItemReq req) {
        SubtitleItem subtitleItem = new SubtitleItem();
        BeanUtils.copyProperties(req, subtitleItem);
        subtitleItem.setCreateTime(java.time.LocalDateTime.now());
        subtitleItem.setUpdateTime(java.time.LocalDateTime.now());
        subtitleItemMapper.insert(subtitleItem);
        return subtitleItem.getId();
    }

    @Override
    public void update(SubtitleItemReq req, Long id) {
        SubtitleItem subtitleItem = new SubtitleItem();
        BeanUtils.copyProperties(req, subtitleItem);
        subtitleItem.setId(id);
        subtitleItem.setUpdateTime(java.time.LocalDateTime.now());
        subtitleItemMapper.updateById(subtitleItem);
    }

    private SubtitleItemResp convertToResp(SubtitleItem subtitleItem) {
        SubtitleItemResp resp = new SubtitleItemResp();
        BeanUtils.copyProperties(subtitleItem, resp);
        return resp;
    }

    @Override
    public List<Long> batchAdd(List<SubtitleItemReq> reqList) {
        if (reqList == null || reqList.isEmpty()) {
            return Collections.emptyList();
        }

        List<Long> ids = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();

        // 遍历逐个插入，获取每个id
        for (SubtitleItemReq req : reqList) {
            SubtitleItem subtitleItem = new SubtitleItem();
            BeanUtils.copyProperties(req, subtitleItem);
            subtitleItem.setCreateTime(now);
            subtitleItem.setUpdateTime(now);
            // 插入并获取id
            save(subtitleItem);
            ids.add(subtitleItem.getId());
        }

        return ids;
    }
}