package com.usky.rule.controller.web;

import com.usky.common.core.bean.ApiResult;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.log.annotation.Log;
import com.usky.common.log.enums.BusinessType;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.rule.domain.RuleEngine;
import com.usky.rule.domain.RuleEngineCondition;
import com.usky.rule.service.RuleEngineConditionService;
import com.usky.rule.service.RuleEngineService;
import com.usky.rule.vo.RuleEngineConfigDTO;
import com.usky.rule.vo.RuleEngineDTO;
import com.usky.rule.vo.RuleEnginePageRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据规则引擎界面 API（表 rule_engine）
 * 含：配置规则、条件下拉、触发
 */
@Api(tags = "数据规则引擎")
@RestController
@RequestMapping("/ruleEngine")
public class RuleEngineController {
    private final RuleEngineService ruleEngineService;
    private final RuleEngineConditionService ruleEngineConditionService;

    public RuleEngineController(RuleEngineService ruleEngineService,
                               RuleEngineConditionService ruleEngineConditionService) {
        this.ruleEngineService = ruleEngineService;
        this.ruleEngineConditionService = ruleEngineConditionService;
    }

    @ApiOperation("新增规则引擎")
    @PostMapping
    public ApiResult<Void> add(@RequestBody RuleEngine ruleEngine) {
        ruleEngineService.add(ruleEngine);
        return ApiResult.success();
    }

    @ApiOperation("修改规则引擎")
    @PutMapping
    public ApiResult<Void> edit(@RequestBody RuleEngine ruleEngine) {
        ruleEngineService.update(ruleEngine);
        return ApiResult.success();
    }

    @ApiOperation("删除规则引擎")
    @DeleteMapping("/{id}")
    public ApiResult<Void> remove(@PathVariable("id") Long id) {
        ruleEngineService.remove(id);
        return ApiResult.success();
    }

    @ApiOperation("规则引擎详情")
    @GetMapping("/{id}")
    public ApiResult<RuleEngine> detail(@PathVariable("id") Long id) {
        return ApiResult.success(ruleEngineService.getById(id));
    }

    @ApiOperation("规则引擎分页")
    @PostMapping("/page")
    public ApiResult<CommonPage<RuleEngine>> page(@RequestBody RuleEnginePageRequest request) {
        return ApiResult.success(ruleEngineService.pageList(request));
    }

    @ApiOperation("查询多条数据（条件同分页，不分页，对应 leo list 接口）")
    @PostMapping("/list")
    public ApiResult<List<RuleEngine>> list(@RequestBody RuleEnginePageRequest request) {
        return ApiResult.success(ruleEngineService.list(request));
    }

    @ApiOperation("规则启停")
    @PutMapping("/{id}/status")
    public ApiResult<Void> status(@PathVariable("id") Long id, @RequestParam("status") Integer status) {
        ruleEngineService.updateStatus(id, status);
        return ApiResult.success();
    }


    // --------------- 配置规则、条件下拉、触发 ---------------

    @ApiOperation("配置规则")
    @PostMapping("/ruleEngineConfig")
    public ApiResult<Void> ruleEngineConfig(@RequestBody RuleEngineDTO dto) {
        ruleEngineService.ruleEngineConfig(dto);
        return ApiResult.success();
    }

    @ApiOperation("条件下拉")
    @GetMapping("/conditions")
    public ApiResult<List<RuleEngineCondition>> conditions(
            @ApiParam(value = "类型：1 触发条件，2 约束条件，不传则返回全部") @RequestParam(required = false) Integer type) {
        List<RuleEngineCondition> list = ruleEngineConditionService.listConditions(type);
        return ApiResult.success(list);
    }

    @ApiOperation("手动触发规则")
    @PostMapping("/{id}/manualTrigger")
    public ApiResult<Void> manualTrigger(@PathVariable("id") Long id) {
        ruleEngineService.manualTrigger(id);
        return ApiResult.success();
    }

    @ApiOperation("获取规则完整配置")
    @GetMapping("/{id}/getEngineConfig")
    public ApiResult<RuleEngineConfigDTO> getEngineConfig(@PathVariable("id") Long id) {
        RuleEngineConfigDTO config = ruleEngineService.getEngineConfig(id);
        return ApiResult.success(config);
    }
}
