package tech.easyflow.admin.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.mybatisflex.core.paginate.Page;
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.common.domain.Result;
import tech.easyflow.common.satoken.util.SaTokenUtil;
import tech.easyflow.common.web.jsonbody.JsonBody;
import tech.easyflow.approval.entity.vo.ApprovalActionRequest;
import tech.easyflow.approval.entity.vo.ApprovalInstanceDetailVo;
import tech.easyflow.approval.entity.vo.ApprovalInstancePageVo;
import tech.easyflow.approval.service.ApprovalInstanceService;
import tech.easyflow.approval.service.ApprovalQueryService;

import javax.annotation.Resource;
import java.math.BigInteger;

/**
 * 审批实例控制器。
 */
@RestController
@RequestMapping("/api/v1/approvalInstance")
public class ApprovalInstanceController {

    @Resource
    private ApprovalQueryService approvalQueryService;

    @Resource
    private ApprovalInstanceService approvalInstanceService;

    /**
     * 分页查询待审批列表。
     *
     * @param resourceType 资源类型
     * @param actionType 动作类型
     * @param keyword 关键词
     * @param pageNumber 页码
     * @param pageSize 每页数量
     * @return 分页结果
     */
    @GetMapping("/pendingPage")
    @SaCheckPermission("/api/v1/approvalInstance/query")
    public Result<Page<ApprovalInstancePageVo>> pendingPage(String resourceType, String actionType, String keyword,
                                                            Long pageNumber, Long pageSize) {
        return Result.ok(approvalQueryService.pendingPage(resourceType, actionType, keyword, pageNumber, pageSize));
    }

    /**
     * 分页查询已审批列表。
     *
     * @param resourceType 资源类型
     * @param actionType 动作类型
     * @param keyword 关键词
     * @param pageNumber 页码
     * @param pageSize 每页数量
     * @return 分页结果
     */
    @GetMapping("/processedPage")
    @SaCheckPermission("/api/v1/approvalInstance/query")
    public Result<Page<ApprovalInstancePageVo>> processedPage(String resourceType, String actionType, String keyword,
                                                              Long pageNumber, Long pageSize) {
        return Result.ok(approvalQueryService.processedPage(resourceType, actionType, keyword, pageNumber, pageSize));
    }

    /**
     * 分页查询我发起的审批列表。
     *
     * @param resourceType 资源类型
     * @param actionType 动作类型
     * @param keyword 关键词
     * @param pageNumber 页码
     * @param pageSize 每页数量
     * @return 分页结果
     */
    @GetMapping("/initiatedPage")
    @SaCheckPermission("/api/v1/approvalInstance/query")
    public Result<Page<ApprovalInstancePageVo>> initiatedPage(String resourceType, String actionType, String keyword,
                                                              Long pageNumber, Long pageSize) {
        return Result.ok(approvalQueryService.initiatedPage(resourceType, actionType, keyword, pageNumber, pageSize));
    }

    /**
     * 查询审批实例详情。
     *
     * @param id 审批实例ID
     * @return 详情结果
     */
    @GetMapping("/detail")
    @SaCheckPermission("/api/v1/approvalInstance/query")
    public Result<ApprovalInstanceDetailVo> detail(BigInteger id) {
        return Result.ok(approvalQueryService.detail(id));
    }

    /**
     * 通过审批。
     *
     * @param request 审批动作请求
     * @return 处理结果
     */
    @PostMapping("/approve")
    @SaCheckPermission("/api/v1/approvalInstance/approve")
    public Result<Void> approve(@JsonBody ApprovalActionRequest request) {
        approvalInstanceService.approve(request.getInstanceId(), request.getComment(), currentOperatorId());
        return Result.ok();
    }

    /**
     * 驳回审批。
     *
     * @param request 审批动作请求
     * @return 处理结果
     */
    @PostMapping("/reject")
    @SaCheckPermission("/api/v1/approvalInstance/reject")
    public Result<Void> reject(@JsonBody ApprovalActionRequest request) {
        approvalInstanceService.reject(request.getInstanceId(), request.getComment(), currentOperatorId());
        return Result.ok();
    }

    /**
     * 撤回审批。
     *
     * @param request 审批动作请求
     * @return 处理结果
     */
    @PostMapping("/revoke")
    @SaCheckPermission("/api/v1/approvalInstance/revoke")
    public Result<Void> revoke(@JsonBody ApprovalActionRequest request) {
        approvalInstanceService.revoke(request.getInstanceId(), request.getComment(), currentOperatorId());
        return Result.ok();
    }

    private BigInteger currentOperatorId() {
        return SaTokenUtil.getLoginAccount().getId();
    }
}
