package tech.easyflow.admin.controller.agent;

import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.easyflow.agent.entity.Agent;
import tech.easyflow.agent.entity.AgentCategory;
import tech.easyflow.agent.mapper.AgentMapper;
import tech.easyflow.agent.service.AgentCategoryService;
import tech.easyflow.common.annotation.UsePermission;
import tech.easyflow.common.domain.Result;
import tech.easyflow.common.web.controller.BaseCurdController;
import tech.easyflow.common.web.exceptions.BusinessException;
import tech.easyflow.system.entity.vo.RoleCategoryAccessSnapshot;
import tech.easyflow.system.enums.CategoryResourceType;
import tech.easyflow.system.service.CategoryPermissionService;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * Agent 分类管理控制器。
 */
@RestController
@RequestMapping("/api/v1/agentCategory")
@UsePermission(moduleName = "/api/v1/agent")
public class AgentCategoryController extends BaseCurdController<AgentCategoryService, AgentCategory> {

    @Resource
    private AgentMapper agentMapper;
    @Resource
    private CategoryPermissionService categoryPermissionService;

    /**
     * 创建 Agent 分类管理控制器。
     *
     * @param service Agent 分类服务
     */
    public AgentCategoryController(AgentCategoryService service) {
        super(service);
    }

    /**
     * 查询当前用户可见的 Agent 分类。
     *
     * @param entity 查询条件
     * @param asTree 是否转树
     * @param sortKey 排序字段
     * @param sortType 排序方式
     * @return 可见分类列表
     */
    @GetMapping("visibleList")
    public Result<List<AgentCategory>> visibleList(AgentCategory entity, Boolean asTree, String sortKey, String sortType) {
        QueryWrapper queryWrapper = QueryWrapper.create(entity, buildOperators(entity));
        RoleCategoryAccessSnapshot access = categoryPermissionService.getCurrentAccess(CategoryResourceType.AGENT.getCode());
        if (access.isRestricted()) {
            if (access.getCategoryIds().isEmpty()) {
                return Result.ok(Collections.emptyList());
            }
            queryWrapper.in("id", access.getCategoryIds());
        }
        queryWrapper.orderBy(buildOrderBy(sortKey, sortType, getDefaultOrderBy()));
        return Result.ok(service.list(queryWrapper));
    }

    /**
     * 删除分类前校验是否仍被 Agent 使用。
     *
     * @param ids 分类 ID 集合
     * @return 校验结果
     */
    @Override
    protected Result<?> onRemoveBefore(Collection<Serializable> ids) {
        for (Serializable id : ids) {
            QueryWrapper queryWrapper = QueryWrapper.create().eq(Agent::getCategoryId, id);
            List<Agent> agents = agentMapper.selectListByQuery(queryWrapper);
            if (agents != null && !agents.isEmpty()) {
                throw new BusinessException("请先删除该分类下的所有 Agent");
            }
        }
        return super.onRemoveBefore(ids);
    }
}
