package com.seer.teach.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seer.teach.admin.convert.AdminAiModelConvert;
import com.seer.teach.admin.request.AdminAiModelPageReq;
import com.seer.teach.admin.request.AdminAiModelQueryReq;
import com.seer.teach.admin.request.AdminAiModelReq;
import com.seer.teach.admin.response.AdminAiModelResp;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.common.utils.AssertUtils;
import com.seer.teach.common.utils.PageConverterUtils;
import com.seer.teach.teacher.module.entity.AiModelEntity;
import com.seer.teach.teacher.service.IAiModelService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

@Slf4j
@RequiredArgsConstructor
@Service
public class AdminAiModelService {

    private final IAiModelService aiModelService;

    public PageListBean<AdminAiModelResp> pageList(AdminAiModelPageReq pageRequest) {
        log.info("分页查询AI模型列表，请求参数: {}", pageRequest);
        IPage<AiModelEntity> page = new Page<>(pageRequest.getPageNo(), pageRequest.getPageSize());

        IPage<AiModelEntity> result = aiModelService.page(page, new LambdaQueryWrapper<AiModelEntity>()
                .eq(StringUtils.hasText(pageRequest.getModelName()), AiModelEntity::getModelName, pageRequest.getModelName())
                .eq(StringUtils.hasText(pageRequest.getModelType()), AiModelEntity::getModelType, pageRequest.getModelType())
                .eq(StringUtils.hasText(pageRequest.getPlatform()), AiModelEntity::getPlatform, pageRequest.getPlatform())
                .eq(StringUtils.hasText(pageRequest.getModelIdentifier()), AiModelEntity::getModelIdentifier, pageRequest.getModelIdentifier())
                .eq(Objects.nonNull(pageRequest.getEnabled()), AiModelEntity::getEnabled, pageRequest.getEnabled())
        );
        
        log.info("分页查询AI模型列表完成，查询到 {} 条记录", result.getRecords().size());
        return PageConverterUtils.convertPageListBean(result, AdminAiModelConvert.INSTANCE::entityListToResponseList);
    }

    public List<AdminAiModelResp> list(AdminAiModelQueryReq queryParam) {
        log.info("查询AI模型列表，请求参数: {}", queryParam);
        List<AiModelEntity> result = aiModelService.list(new LambdaQueryWrapper<AiModelEntity>()
                .eq(Objects.nonNull(queryParam.getId()), AiModelEntity::getId, queryParam.getId())
                .eq(Objects.nonNull(queryParam.getModelName()), AiModelEntity::getModelName, queryParam.getModelName())
                .eq(StringUtils.hasText(queryParam.getModelType()), AiModelEntity::getModelType, queryParam.getModelType())
                .eq(StringUtils.hasText(queryParam.getPlatform()), AiModelEntity::getPlatform, queryParam.getPlatform())
                .eq(Objects.nonNull(queryParam.getModelIdentifier()), AiModelEntity::getModelIdentifier, queryParam.getModelIdentifier())
                .eq(Objects.nonNull(queryParam.getDescription()), AiModelEntity::getDescription, queryParam.getDescription())
                .eq(Objects.nonNull(queryParam.getMaxTokens()), AiModelEntity::getMaxTokens, queryParam.getMaxTokens())
                .eq(Objects.nonNull(queryParam.getEnabled()), AiModelEntity::getEnabled, queryParam.getEnabled())
                .eq(Objects.nonNull(queryParam.getConfig()), AiModelEntity::getConfig, queryParam.getConfig())
        );
        log.info("查询AI模型列表完成，查询到 {} 条记录", result.size());
        return AdminAiModelConvert.INSTANCE.entityListToResponseList(result);
    }

    public AdminAiModelResp getDetail(Integer id) {
        log.info("根据ID获取AI模型详情，ID: {}", id);
        AiModelEntity result = aiModelService.getById(id);
        log.info("获取AI模型详情完成，ID: {}，结果: {}", id, result);
        return AdminAiModelConvert.INSTANCE.entityToResponse(result);
    }

    public boolean save(AdminAiModelReq aiModel) {
        log.info("保存AI模型，请求参数: {}", aiModel);
        AiModelEntity entity = AdminAiModelConvert.INSTANCE.reqToEntity(aiModel);
        
        // 校验模型标识符唯一性
        if (StringUtils.hasText(entity.getModelIdentifier())) {
            long count = aiModelService.count(new LambdaQueryWrapper<AiModelEntity>()
                    .eq(AiModelEntity::getModelIdentifier, entity.getModelIdentifier()));
            AssertUtils.isTrue(count == 0, ResultCodeEnum.MODEL_IDENTIFIER_ALREADY_EXISTS);
        }
        
        boolean saved = aiModelService.save(entity);
        log.info("保存AI模型完成，结果: {}，保存的实体ID: {}", saved, entity.getId());
        return saved;
    }

    public boolean updateById(AdminAiModelReq aiModel) {
        log.info("根据ID更新AI模型，请求参数: {}", aiModel);
        AiModelEntity entity = AdminAiModelConvert.INSTANCE.reqToEntity(aiModel);
        boolean updated = aiModelService.updateById(entity);
        log.info("根据ID更新AI模型完成，结果: {}，更新的实体ID: {}", updated, entity.getId());
        return updated;
    }

    public boolean deleteById(Integer id) {
        log.info("根据ID删除AI模型，ID: {}", id);
        boolean updated = aiModelService.removeById(id);
        log.info("根据ID删除AI模型完成，结果: {}，删除的实体ID: {}", updated, id);
        return updated;
    }
}