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.LibraryItemMapper;
import top.continew.admin.business.model.entity.LibraryItem;
import top.continew.admin.business.model.query.LibraryItemPageQuery;
import top.continew.admin.business.model.query.LibraryItemQuery;
import top.continew.admin.business.model.req.LibraryItemReq;
import top.continew.admin.business.model.resp.LibraryItemResp;
import top.continew.admin.business.service.LibraryItemService;
import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.extension.crud.service.impl.BaseServiceImpl;

/**
 * 库中项目服务实现
 *
 * @author author
 * @since 2026-01-07
 */
@Service
@RequiredArgsConstructor
public class LibraryItemServiceImpl extends BaseServiceImpl<LibraryItemMapper, LibraryItem, LibraryItemResp, LibraryItemResp, LibraryItemQuery, LibraryItemReq> implements LibraryItemService {

    private final LibraryItemMapper libraryItemMapper;

    @Override
    public PageResp<LibraryItemResp> pageList(LibraryItemQuery query, LibraryItemPageQuery pageQuery) {
        LambdaQueryWrapper<LibraryItem> wrapper = new LambdaQueryWrapper<>();
        if (query.getUserId() != null) {
            wrapper.eq(LibraryItem::getUserId, query.getUserId());
        }
        if (query.getIsCustom() != null) {
            wrapper.eq(LibraryItem::getIsCustom, query.getIsCustom());
        }
        if (query.getType() != null) {
            wrapper.eq(LibraryItem::getType, query.getType());
        }
        if (query.getName() != null) {
            wrapper.like(LibraryItem::getName, query.getName());
        }
        // 年龄筛选
        if (query.getAge() != null) {
            wrapper.eq(LibraryItem::getAge, query.getAge());
        }
        // 性别筛选
        if (query.getGender() != null) {
            wrapper.eq(LibraryItem::getGender, query.getGender());
        }
        // 场景筛选
        if (query.getScene() != null) {
            wrapper.eq(LibraryItem::getScene, query.getScene());
        }
        // 语种筛选
        if (query.getLanguage() != null) {
            wrapper.eq(LibraryItem::getLanguage, query.getLanguage());
        }

        // 默认按createTime倒序排列
        wrapper.orderByDesc(LibraryItem::getCreateTime);

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

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

    @Override
    public Long add(LibraryItemReq req) {
        LibraryItem libraryItem = new LibraryItem();
        BeanUtils.copyProperties(req, libraryItem);
        libraryItem.setCreateTime(java.time.LocalDateTime.now());
        libraryItem.setUpdateTime(java.time.LocalDateTime.now());
        libraryItemMapper.insert(libraryItem);
        return libraryItem.getId();
    }

    private LibraryItemResp convertToResp(LibraryItem libraryItem) {
        LibraryItemResp resp = new LibraryItemResp();
        BeanUtils.copyProperties(libraryItem, resp);
        return resp;
    }
}