package com.cool.modules.aiconfig.service.impl;

import cn.hutool.core.util.StrUtil;
import com.cool.core.base.BaseServiceImpl;
import com.cool.modules.aiconfig.entity.AiTemplateEntity;
import com.cool.modules.aiconfig.mapper.AiTemplateMapper;
import com.cool.modules.aiconfig.service.AiTemplateService;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * AI模板提示词配置服务实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AiTemplateServiceImpl extends BaseServiceImpl<AiTemplateMapper, AiTemplateEntity> implements AiTemplateService {

    @Override
    public AiTemplateEntity getByTemplateKey(String templateKey) {
        log.info("【AI模板配置】根据模板标识获取模板配置, templateKey: {}", templateKey);

        if (StrUtil.isEmpty(templateKey)) {
            log.warn("【AI模板配置】模板标识为空");
            return null;
        }

        QueryWrapper queryWrapper = QueryWrapper.create()
                .eq(AiTemplateEntity::getTemplateKey, templateKey);

        AiTemplateEntity entity = this.getOne(queryWrapper);

        if (entity != null) {
            log.info("【AI模板配置】查询成功, templateId: {}, templateName: {}", entity.getId(), entity.getTemplateName());
        } else {
            log.warn("【AI模板配置】未找到对应的模板配置, templateKey: {}", templateKey);
        }

        return entity;
    }

    @Override
    public List<AiTemplateEntity> getByScene(String scene) {
        log.info("【AI模板配置】根据场景获取模板列表, scene: {}", scene);

        if (StrUtil.isEmpty(scene)) {
            log.warn("【AI模板配置】场景为空");
            return List.of();
        }

        QueryWrapper queryWrapper = QueryWrapper.create()
                .eq(AiTemplateEntity::getScene, scene)
                .eq(AiTemplateEntity::getStatus, 1)
                .orderBy(AiTemplateEntity::getOrderNum, true)
                .orderBy(AiTemplateEntity::getCreateTime, false);

        List<AiTemplateEntity> list = this.list(queryWrapper);

        log.info("【AI模板配置】查询成功, scene: {}, 模板数量: {}", scene, list.size());

        return list;
    }

    @Override
    public List<AiTemplateEntity> getByModelId(Long modelId) {
        log.info("【AI模板配置】根据模型ID获取模板列表, modelId: {}", modelId);

        if (modelId == null) {
            log.warn("【AI模板配置】模型ID为空");
            return List.of();
        }

        QueryWrapper queryWrapper = QueryWrapper.create()
                .eq(AiTemplateEntity::getModelId, modelId)
                .eq(AiTemplateEntity::getStatus, 1)
                .orderBy(AiTemplateEntity::getOrderNum, true)
                .orderBy(AiTemplateEntity::getCreateTime, false);

        List<AiTemplateEntity> list = this.list(queryWrapper);

        log.info("【AI模板配置】查询成功, modelId: {}, 模板数量: {}", modelId, list.size());

        return list;
    }
}
