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.AiModelEntity;
import com.cool.modules.aiconfig.mapper.AiModelMapper;
import com.cool.modules.aiconfig.service.AiModelService;
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 AiModelServiceImpl extends BaseServiceImpl<AiModelMapper, AiModelEntity> implements AiModelService {

    @Override
    public AiModelEntity getByModelKey(String modelKey) {
        log.info("【AI模型配置】根据模型标识获取模型配置, modelKey: {}", modelKey);

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

        QueryWrapper queryWrapper = QueryWrapper.create()
                .eq(AiModelEntity::getModelKey, modelKey);

        AiModelEntity entity = this.getOne(queryWrapper);

        if (entity != null) {
            log.info("【AI模型配置】查询成功, modelId: {}, modelName: {}", entity.getId(), entity.getModelName());
        } else {
            log.warn("【AI模型配置】未找到对应的模型配置, modelKey: {}", modelKey);
        }

        return entity;
    }

    @Override
    public List<AiModelEntity> getEnabledModels() {
        log.info("【AI模型配置】获取所有启用的模型列表");

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

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

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

        return list;
    }
}
