package com.pavis.admin.aigc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pavis.admin.aigc.mapper.AgentKnowledgeMapper;
import com.pavis.admin.aigc.mapper.AgentMapper;
import com.pavis.admin.aigc.mapper.AgentToolMapper;
import com.pavis.admin.aigc.mapper.KnowledgeMapper;
import com.pavis.admin.aigc.mapper.ToolMapper;
import com.pavis.admin.aigc.model.entity.AgentDO;
import com.pavis.admin.aigc.model.entity.AgentKnowledgeDO;
import com.pavis.admin.aigc.model.entity.AgentToolDO;
import com.pavis.admin.aigc.model.entity.KnowledgeDO;
import com.pavis.admin.aigc.model.entity.ToolDO;
import com.pavis.admin.aigc.model.query.AgentQuery;
import com.pavis.admin.aigc.model.req.AgentReq;
import com.pavis.admin.aigc.model.req.AgentToolReq;
import com.pavis.admin.aigc.model.resp.AgentDetailResp;
import com.pavis.admin.aigc.model.resp.AgentResp;
import com.pavis.admin.aigc.service.AgentService;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.continew.starter.extension.crud.service.BaseServiceImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * 智能体业务实现
 *
 * @author semi
 * @since 2025/05/26 17:28
 */
@Service
@RequiredArgsConstructor
public class AgentServiceImpl extends BaseServiceImpl<AgentMapper, AgentDO, AgentResp, AgentDetailResp, AgentQuery, AgentReq> implements AgentService {
    @Resource
    private AgentKnowledgeMapper agentKnowledgeMapper;

    @Resource
    private KnowledgeMapper knowledgeMapper;

    @Resource
    private AgentToolMapper agentToolMapper;

    @Resource
    private ToolMapper toolMapper;

    @Override
    public AgentResp getByCode(String agentCode) {
        return baseMapper.selectByCode(agentCode);
    }

    @Override
    public List<AgentResp> getAll() {
        return baseMapper.selectAgentList();
    }

    @Override
    public KnowledgeDO selAgentKnowList(Long agentId) {
        // 获取knowledgeId
        AgentKnowledgeDO agentKnowledge = agentKnowledgeMapper.selectOne(new QueryWrapper<AgentKnowledgeDO>()
                .eq("agent_id", agentId));
        // 获取知识库详情
        return knowledgeMapper.selectById(agentKnowledge.getKnowledgeId());
    }

    @Override
    public List<ToolDO> selTool(Long agentId) {
        List<ToolDO> list = new ArrayList<>();
        List<AgentToolDO> agentToolList = agentToolMapper.selectList(new QueryWrapper<AgentToolDO>()
                .eq("agent_id", agentId));
        if (!agentToolList.isEmpty()) {
            for (AgentToolDO agentToolDO : agentToolList) {
                ToolDO toolDO = toolMapper.selectById(agentToolDO.getToolId());
                list.add(toolDO);
            }
        }
        return list;
    }

    @Override
    public void delToolToAgent(AgentToolReq agentToolReq){
        agentToolMapper.delete(new QueryWrapper<AgentToolDO>()
                .eq("tool_id",agentToolReq.getToolId())
                .eq("agent_id",agentToolReq.getAgentId())
        );
    }
}