package tech.easyflow.admin.controller.skill;

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.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.skill.entity.Skill;
import tech.easyflow.skill.entity.SkillCategory;
import tech.easyflow.skill.mapper.SkillMapper;
import tech.easyflow.skill.service.SkillCategoryService;
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;

/**
 * Skill 分类管理控制器。
 */
@RestController
@RequestMapping("/api/v1/skillCategory")
@UsePermission(moduleName = "/api/v1/skill")
public class SkillCategoryController extends BaseCurdController<SkillCategoryService, SkillCategory> {

    @Resource
    private SkillMapper skillMapper;
    @Resource
    private CategoryPermissionService categoryPermissionService;

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

    /**
     * 查询当前用户可见的 Skill 分类。
     *
     * @param entity 查询条件
     * @param asTree 是否转树
     * @param sortKey 排序字段
     * @param sortType 排序方式
     * @return 可见分类列表
     */
    @GetMapping("visibleList")
    public Result<List<SkillCategory>> visibleList(SkillCategory entity, Boolean asTree, String sortKey, String sortType) {
        QueryWrapper queryWrapper = QueryWrapper.create(entity, buildOperators(entity));
        RoleCategoryAccessSnapshot access = categoryPermissionService.getCurrentAccess(CategoryResourceType.SKILL.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));
    }

    /**
     * 删除分类前校验是否仍被 Skill 使用。
     *
     * @param ids 分类 ID 集合
     * @return 校验结果
     */
    @Override
    protected Result<?> onRemoveBefore(Collection<Serializable> ids) {
        for (Serializable id : ids) {
            List<Skill> skills = skillMapper.selectListByQuery(QueryWrapper.create().eq(Skill::getCategoryId, id));
            if (skills != null && !skills.isEmpty()) {
                throw new BusinessException("请先迁移或删除该分类下的 Skill");
            }
            List<SkillCategory> children = service.list(QueryWrapper.create().eq(SkillCategory::getParentId, id));
            if (children != null && !children.isEmpty()) {
                throw new BusinessException("请先删除子分类");
            }
        }
        return super.onRemoveBefore(ids);
    }
}

