package tech.easyflow.admin.controller.ai;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.easyflow.ai.entity.FaqCategory;
import tech.easyflow.ai.service.FaqCategoryService;
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.common.web.jsonbody.JsonBody;
import tech.easyflow.system.enums.CategoryResourceType;
import tech.easyflow.system.enums.ResourceAction;
import tech.easyflow.system.enums.ResourceLookup;
import tech.easyflow.system.permission.resource.RequireResourceAccess;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.List;

@RestController
@RequestMapping("/api/v1/faqCategory")
@UsePermission(moduleName = "/api/v1/documentCollection")
public class FaqCategoryController extends BaseCurdController<FaqCategoryService, FaqCategory> {

    public FaqCategoryController(FaqCategoryService service) {
        super(service);
    }

    @Override
    @GetMapping("list")
    @SaCheckPermission("/api/v1/documentCollection/query")
    @RequireResourceAccess(
            resource = CategoryResourceType.KNOWLEDGE,
            action = ResourceAction.READ,
            lookup = ResourceLookup.KNOWLEDGE_ID,
            idExpr = "#entity == null ? null : #entity.collectionId",
            denyMessage = "无权限访问知识库"
    )
    public Result<List<FaqCategory>> list(FaqCategory entity, Boolean asTree, String sortKey, String sortType) {
        BigInteger collectionId = entity == null ? null : entity.getCollectionId();
        if (collectionId == null) {
            throw new BusinessException("知识库ID不能为空");
        }
        return Result.ok(service.listByCollection(collectionId, asTree));
    }

    @Override
    @PostMapping("save")
    @SaCheckPermission("/api/v1/documentCollection/save")
    @RequireResourceAccess(
            resource = CategoryResourceType.KNOWLEDGE,
            action = ResourceAction.MANAGE,
            lookup = ResourceLookup.KNOWLEDGE_ID,
            idExpr = "#entity.collectionId",
            denyMessage = "无权限管理知识库"
    )
    public Result<?> save(@JsonBody FaqCategory entity) {
        return Result.ok(service.saveCategory(entity));
    }

    @Override
    @PostMapping("update")
    @SaCheckPermission("/api/v1/documentCollection/save")
    @RequireResourceAccess(
            resource = CategoryResourceType.KNOWLEDGE,
            action = ResourceAction.MANAGE,
            lookup = ResourceLookup.FAQ_CATEGORY_ID,
            idExpr = "#entity.id",
            denyMessage = "无权限管理知识库"
    )
    public Result<?> update(@JsonBody FaqCategory entity) {
        return Result.ok(service.updateCategory(entity));
    }

    @Override
    @PostMapping("remove")
    @SaCheckPermission("/api/v1/documentCollection/remove")
    @RequireResourceAccess(
            resource = CategoryResourceType.KNOWLEDGE,
            action = ResourceAction.MANAGE,
            lookup = ResourceLookup.FAQ_CATEGORY_ID,
            idExpr = "#id",
            denyMessage = "无权限管理知识库"
    )
    public Result<?> remove(@JsonBody(value = "id", required = true) Serializable id) {
        return Result.ok(service.removeCategory(new BigInteger(String.valueOf(id))));
    }

    @Override
    protected String getDefaultOrderBy() {
        return "sort_no asc";
    }
}
