package cn.iocoder.yudao.module.system.controller.admin.dify;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.system.controller.admin.dify.vo.DifyWorkFlowRecordPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dify.vo.DifyWorkFlowRecordRespVO;
import cn.iocoder.yudao.module.system.controller.admin.dify.vo.DifyWorkFlowRecordSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dify.vo.DifyWorkFlowRunReqVO;
import cn.iocoder.yudao.module.system.service.dify.DifyWorkFlowRecordService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - Dify 工作流记录")
@RestController
@RequestMapping("/dify-work-flow-record")
@Validated
public class DifyWorkFlowRecordController {

    @Resource
    private DifyWorkFlowRecordService difyWorkFlowRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建 Dify 工作流记录")
    public CommonResult<Long> create(@Valid @RequestBody DifyWorkFlowRecordSaveReqVO createReqVO) {
        return success(difyWorkFlowRecordService.create(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新 Dify 工作流记录")
    public CommonResult<Boolean> update(@Valid @RequestBody DifyWorkFlowRecordSaveReqVO updateReqVO) {
        difyWorkFlowRecordService.update(updateReqVO);
        return success(Boolean.TRUE);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除 Dify 工作流记录")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        difyWorkFlowRecordService.delete(id);
        return success(Boolean.TRUE);
    }

    @GetMapping("/get")
    @Operation(summary = "获得 Dify 工作流记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    public CommonResult<DifyWorkFlowRecordRespVO> get(@RequestParam("id") Long id) {
        return success(difyWorkFlowRecordService.get(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得 Dify 工作流记录分页")
    public CommonResult<PageResult<DifyWorkFlowRecordRespVO>> getPage(@Valid DifyWorkFlowRecordPageReqVO pageReqVO) {
        return success(difyWorkFlowRecordService.getPage(pageReqVO));
    }

    @PostMapping("/workflow-run")
    @Operation(summary = "执行 Dify 工作流")
    @TenantIgnore
    public CommonResult<JSONObject> workflowRun(@Valid @RequestBody DifyWorkFlowRunReqVO reqVO) {
        return success(difyWorkFlowRecordService.workflowRun(reqVO));
    }
}
