package com.yolo.keyboard.service.aicompanion;

import com.yolo.keyboard.controller.admin.aicompanion.vo.KeyboardAiCompanionPageReqVO;
import com.yolo.keyboard.controller.admin.aicompanion.vo.KeyboardAiCompanionSaveReqVO;
import com.yolo.keyboard.dal.dataobject.aicompanion.AiCompanionI18nDO;
import com.yolo.keyboard.dal.dataobject.aicompanion.KeyboardAiCompanionDO;
import com.yolo.keyboard.dal.mysql.aicompanion.AiCompanionI18nMapper;
import com.yolo.keyboard.dal.mysql.aicompanion.KeyboardAiCompanionMapper;
import com.yolo.keyboard.framework.common.pojo.PageParam;
import com.yolo.keyboard.framework.common.pojo.PageResult;
import com.yolo.keyboard.framework.common.util.object.BeanUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static com.yolo.keyboard.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yolo.keyboard.module.infra.enums.ErrorCodeConstants.AI_COMPANION_I18N_NOT_EXISTS;
import static com.yolo.keyboard.module.infra.enums.ErrorCodeConstants.AI_COMPANION_NOT_EXISTS;

/**
 * AI陪聊角色表，用于定义恋爱/陪伴型虚拟角色的基础信息与人设 Service 实现类
 *
 * @author ziin
 */
@Service
@Validated
public class KeyboardAiCompanionServiceImpl implements KeyboardAiCompanionService {

    @Resource
    private KeyboardAiCompanionMapper aiCompanionMapper;
    @Resource
    private AiCompanionI18nMapper aiCompanionI18nMapper;

    @Override
    public Long createAiCompanion(KeyboardAiCompanionSaveReqVO createReqVO) {
        // 插入
        KeyboardAiCompanionDO aiCompanion = BeanUtils.toBean(createReqVO, KeyboardAiCompanionDO.class);
        aiCompanionMapper.insert(aiCompanion);

        // 返回
        return aiCompanion.getId();
    }

    @Override
    public void updateAiCompanion(KeyboardAiCompanionSaveReqVO updateReqVO) {
        // 校验存在
        validateAiCompanionExists(updateReqVO.getId());
        // 更新
        KeyboardAiCompanionDO updateObj = BeanUtils.toBean(updateReqVO, KeyboardAiCompanionDO.class);
        aiCompanionMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAiCompanion(Long id) {
        // 校验存在
        validateAiCompanionExists(id);
        // 删除
        aiCompanionMapper.deleteById(id);

        // 删除子表
        deleteAiCompanionI18nById(id);
    }

    @Override
        @Transactional(rollbackFor = Exception.class)
    public void deleteAiCompanionListByIds(List<Long> ids) {
        // 删除
        aiCompanionMapper.deleteByIds(ids);

    // 删除子表
            deleteAiCompanionI18nByIds(ids);
    }


    private void validateAiCompanionExists(Long id) {
        if (aiCompanionMapper.selectById(id) == null) {
            throw exception(AI_COMPANION_NOT_EXISTS);
        }
    }

    @Override
    public KeyboardAiCompanionDO getAiCompanion(Long id) {
        return aiCompanionMapper.selectById(id);
    }

    @Override
    public PageResult<KeyboardAiCompanionDO> getAiCompanionPage(KeyboardAiCompanionPageReqVO pageReqVO) {
        return aiCompanionMapper.selectPage(pageReqVO);
    }

    // ==================== 子表（AI陪聊角色国际化表，用于存储不同语言下的角色名称、一句话描述和详细介绍） ====================

    @Override
    public PageResult<AiCompanionI18nDO> getAiCompanionI18nPage(PageParam pageReqVO, Long id) {
        return aiCompanionI18nMapper.selectPage(pageReqVO, id);
    }

    @Override
    public Long createAiCompanionI18n(AiCompanionI18nDO aiCompanionI18n) {
        aiCompanionI18nMapper.insert(aiCompanionI18n);
        return aiCompanionI18n.getId();
    }

    @Override
    public void updateAiCompanionI18n(AiCompanionI18nDO aiCompanionI18n) {
        // 校验存在
        validateAiCompanionI18nExists(aiCompanionI18n.getId());
        // 更新
        aiCompanionI18nMapper.updateById(aiCompanionI18n);
    }

    @Override
    public void deleteAiCompanionI18n(Long id) {
        // 删除
        aiCompanionI18nMapper.deleteById(id);
    }

	@Override
	public void deleteAiCompanionI18nListByIds(List<Long> ids) {
        // 删除
        aiCompanionI18nMapper.deleteByIds(ids);
	}

    @Override
    public AiCompanionI18nDO getAiCompanionI18n(Long id) {
        return aiCompanionI18nMapper.selectById(id);
    }

    private void validateAiCompanionI18nExists(Long id) {
        if (aiCompanionI18nMapper.selectById(id) == null) {
            throw exception(AI_COMPANION_I18N_NOT_EXISTS);
        }
    }

    private void deleteAiCompanionI18nById(Long id) {
        aiCompanionI18nMapper.deleteById(id);
    }

	private void deleteAiCompanionI18nByIds(List<Long> ids) {
        aiCompanionI18nMapper.deleteByIds(ids);
	}

}