package com.fs.company.controller.company;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.domain.CompanyWorkflow;
import com.fs.company.domain.CompanyWorkflowNodeType;
import com.fs.company.param.CompanyWorkflowSaveParam;
import com.fs.company.param.CompanyWorkflowUpdateBindWCParam;
import com.fs.company.service.ICompanyWorkflowService;
import com.fs.company.vo.OptionVO;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * AI工作流Controller
 *
 * @author fs
 * @date 2026-01-06
 */
@RestController
@RequestMapping("/company/companyWorkflow")
public class CompanyWorkflowController extends BaseController {

    @Autowired
    private ICompanyWorkflowService companyWorkflowService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询AI工作流列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CompanyWorkflow fsAiWorkflow) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        fsAiWorkflow.setCompanyId(loginUser.getCompany().getCompanyId());
        startPage();
        List<CompanyWorkflow> list = companyWorkflowService.selectCompanyWorkflowList(fsAiWorkflow);
        return getDataTable(list);
    }

    /**
     * 导出AI工作流列表
     */
    @Log(title = "AI工作流", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CompanyWorkflow fsAiWorkflow) {
        List<CompanyWorkflow> list = companyWorkflowService.selectCompanyWorkflowList(fsAiWorkflow);
        ExcelUtil<CompanyWorkflow> util = new ExcelUtil<CompanyWorkflow>(CompanyWorkflow.class);
        return util.exportExcel(list, "AI工作流数据");
    }

    /**
     * 获取AI工作流详细信息(包含节点和连线)
     */
    @GetMapping(value = "/{workflowId}")
    public AjaxResult getInfo(@PathVariable("workflowId") Long workflowId) {
        return AjaxResult.success(companyWorkflowService.selectCompanyWorkflowById(workflowId));
    }

    /**
     * 保存AI工作流(新增或更新)
     */
    @Log(title = "AI工作流", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody CompanyWorkflowSaveParam param) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId(loginUser.getUser().getCompanyId());
        param.setCompanyUserId(loginUser.getUser().getUserId());
        Long workflowId = companyWorkflowService.saveCompanyWorkflow(param);
        return AjaxResult.success(workflowId);
    }

    /**
     * 修改AI工作流状态
     */
    @Log(title = "AI工作流", businessType = BusinessType.UPDATE)
    @PutMapping("/status/{workflowId}/{status}")
    public AjaxResult updateStatus(@PathVariable("workflowId") Long workflowId,
                                   @PathVariable("status") Integer status) {
        return toAjax(companyWorkflowService.updateCompanyWorkflowStatus(workflowId, status));
    }

    /**
     * 删除AI工作流
     */
    @Log(title = "AI工作流", businessType = BusinessType.DELETE)
    @DeleteMapping("/{workflowIds}")
    public AjaxResult remove(@PathVariable Long[] workflowIds) {
        return toAjax(companyWorkflowService.deleteCompanyWorkflowByIds(workflowIds));
    }

    /**
     * 复制AI工作流
     */
    @Log(title = "AI工作流", businessType = BusinessType.INSERT)
    @PostMapping("/copy/{workflowId}")
    public AjaxResult copy(@PathVariable("workflowId") Long workflowId) {
        Long newWorkflowId = companyWorkflowService.copyCompanyWorkflow(workflowId);
        if (newWorkflowId != null) {
            return AjaxResult.success(newWorkflowId);
        }
        return AjaxResult.error("复制失败，工作流不存在");
    }

    /**
     * 获取所有启用的节点类型
     */
    @GetMapping("/nodeTypes")
    public AjaxResult getNodeTypes() {
        List<CompanyWorkflowNodeType> list = companyWorkflowService.selectAllEnabledNodeTypes();
        return AjaxResult.success(list);
    }
    /**
     * 导出工作流流程图JSON
     * 包含节点信息、连接顺序、节点类型
     */
//    @Log(title = "AI工作流", businessType = BusinessType.EXPORT)
    @GetMapping("/exportJson/{workflowId}")
    public AjaxResult exportJson(@PathVariable("workflowId") Long workflowId) {
        return AjaxResult.success(companyWorkflowService.exportWorkflowJson(workflowId));
    }
    /**
     * 分页销售
     */
    @GetMapping("/listCompanyUser")
    public AjaxResult listCompanyUser() {
        return AjaxResult.success(companyWorkflowService.listCompanyUser());
    }

    /**
     * 查销售
     */
    @GetMapping("/getCompanyUserById/{companyUserId}")
    public AjaxResult getCompanyUserById(@PathVariable("companyUserId") Long companyUserId) {
        return AjaxResult.success(companyWorkflowService.getCompanyUserById(companyUserId));
    }

//    /**
//     * 查销售是否已经被绑定
//     */
//    @GetMapping("/checkCompanyUserBeUsed/{companyUserId}")
//    public AjaxResult checkCompanyUserBeUsed(@PathVariable("companyUserId") Long companyUserId) {
//        return AjaxResult.success(companyWorkflowService.checkCompanyUserBeUsed(companyUserId));
//    }

    /**
     * 查工作流已绑定的销售
     */
    @GetMapping("/getBindCompanyUserByWorkflowId/{workflowId}")
    public AjaxResult getBindCompanyUserByWorkflowId(@PathVariable("workflowId") Long workflowId) {
        return AjaxResult.success(companyWorkflowService.getBindCompanyUserByWorkflowId(workflowId));
    }

    /**
     * 修改工作流绑定的销售
     */
    @PostMapping("/updateWorkflowBindCompanyUser")
    public AjaxResult updateWorkflowBindCompanyUser(@RequestBody CompanyWorkflowUpdateBindWCParam param) {
        return companyWorkflowService.updateWorkflowBindCompanyUser(param);
    }
    /**
     * 修改工作流绑定的销售
     */
    @GetMapping("/optionList")
    public R optionList() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        List<OptionVO> optionVOS = companyWorkflowService.optionList(loginUser.getCompany().getCompanyId());
        return R.ok().put("data",optionVOS);
    }

}
