package com.tashow.cloud.ai.service.aisample;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tashow.cloud.ai.controller.admin.aisample.vo.AiSampleTagGroupPageReqVO;
import com.tashow.cloud.ai.controller.admin.aisample.vo.AiSampleTagGroupSaveReqVO;
import com.tashow.cloud.ai.dal.dataobject.aisample.AiSampleTagGroupDO;
import com.tashow.cloud.ai.dal.mysql.aisample.AiSampleTagGroupMapper;
import com.tashow.cloud.common.pojo.PageResult;
import com.tashow.cloud.common.util.object.BeanUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

@Service
@Validated
public class AiSampleTagGroupServiceImpl implements AiSampleTagGroupService {

    private static final Integer ENUM_TYPE = 1;

    @Resource
    private AiSampleTagGroupMapper aiSampleTagGroupMapper;

    @Override
    public Long createAiSampleTagGroup(AiSampleTagGroupSaveReqVO createReqVO) {
        AiSampleTagGroupDO group = BeanUtils.toBean(createReqVO, AiSampleTagGroupDO.class);
        aiSampleTagGroupMapper.insert(group);
        return group.getId();
    }

    @Override
    public void updateAiSampleTagGroup(AiSampleTagGroupSaveReqVO updateReqVO) {
        AiSampleTagGroupDO existingGroup = aiSampleTagGroupMapper.selectById(updateReqVO.getId());
        if (isEnumType(existingGroup.getGroupType())
                && !existingGroup.getGroupType().equals(updateReqVO.getGroupType())) {
            throw new IllegalArgumentException("Enum group type cannot be changed");
        }
        AiSampleTagGroupDO group = BeanUtils.toBean(updateReqVO, AiSampleTagGroupDO.class);
        aiSampleTagGroupMapper.updateById(group);
    }

    @Override
    public void deleteAiSampleTagGroup(Long id) {
        AiSampleTagGroupDO group = aiSampleTagGroupMapper.selectById(id);
        if (isEnumType(group.getGroupType())) {
            throw new IllegalArgumentException("Enum group cannot be deleted");
        }
        aiSampleTagGroupMapper.deleteById(id);
    }

    @Override
    public AiSampleTagGroupDO getAiSampleTagGroup(Long id) {
        return aiSampleTagGroupMapper.selectById(id);
    }

    @Override
    public PageResult<AiSampleTagGroupDO> getAiSampleTagGroupPage(AiSampleTagGroupPageReqVO pageReqVO) {
        return aiSampleTagGroupMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AiSampleTagGroupDO> getAiSampleTagGroupList() {
        return aiSampleTagGroupMapper.selectList(new LambdaQueryWrapper<AiSampleTagGroupDO>()
                .orderByDesc(AiSampleTagGroupDO::getId));
    }

    private boolean isEnumType(Integer type) {
        return ENUM_TYPE.equals(type);
    }
}
