package com.contract.review.controller;

import com.contract.review.entity.ContractRisk;
import com.contract.review.service.ContractRiskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 合同风险控制器
 */
@RestController
@RequestMapping("/api/risks")
@Api(tags = "合同风险管理")
public class ContractRiskController {
    
    @Autowired
    private ContractRiskService contractRiskService;
    
    /**
     * 获取风险列表
     * @param contractId 合同ID
     * @param riskLevel 风险等级
     * @param riskType 风险类型
     * @return 风险列表
     */
    @GetMapping
    @ApiOperation("获取风险列表")
    public Map<String, Object> getRiskList(
            @RequestParam(required = false) Long contractId,
            @RequestParam(required = false) String riskLevel,
            @RequestParam(required = false) String riskType) {
        Map<String, Object> params = new HashMap<>();
        params.put("contractId", contractId);
        params.put("riskLevel", riskLevel);
        params.put("riskType", riskType);
        
        List<ContractRisk> riskList = contractRiskService.getRiskList(params);
        
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("message", "success");
        result.put("data", riskList);
        
        return result;
    }
    
    /**
     * 根据ID获取风险
     * @param id 风险ID
     * @return 风险详情
     */
    @GetMapping("/{id}")
    @ApiOperation("获取风险详情")
    public Map<String, Object> getRiskById(@PathVariable Long id) {
        ContractRisk risk = contractRiskService.getRiskById(id);
        
        Map<String, Object> result = new HashMap<>();
        if (risk != null) {
            result.put("code", 200);
            result.put("message", "success");
            result.put("data", risk);
        } else {
            result.put("code", 404);
            result.put("message", "风险不存在");
        }
        
        return result;
    }
    
    /**
     * 新增风险
     * @param risk 风险
     * @return 结果
     */
    @PostMapping
    @ApiOperation("新增风险")
    public Map<String, Object> addRisk(@RequestBody ContractRisk risk) {
        Long riskId = contractRiskService.addRisk(risk);
        
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("message", "success");
        result.put("data", riskId);
        
        return result;
    }
    
    /**
     * 更新风险
     * @param id 风险ID
     * @param risk 风险
     * @return 结果
     */
    @PutMapping("/{id}")
    @ApiOperation("更新风险")
    public Map<String, Object> updateRisk(@PathVariable Long id, @RequestBody ContractRisk risk) {
        risk.setId(id);
        int rows = contractRiskService.updateRisk(risk);
        
        Map<String, Object> result = new HashMap<>();
        if (rows > 0) {
            result.put("code", 200);
            result.put("message", "success");
        } else {
            result.put("code", 404);
            result.put("message", "风险不存在");
        }
        
        return result;
    }
    
    /**
     * 删除风险
     * @param id 风险ID
     * @return 结果
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除风险")
    public Map<String, Object> deleteRisk(@PathVariable Long id) {
        int rows = contractRiskService.deleteRisk(id);
        
        Map<String, Object> result = new HashMap<>();
        if (rows > 0) {
            result.put("code", 200);
            result.put("message", "success");
        } else {
            result.put("code", 404);
            result.put("message", "风险不存在");
        }
        
        return result;
    }
    
    /**
     * 更新风险状态
     * @param id 风险ID
     * @param status 状态：0-正常，1-忽略
     * @return 结果
     */
    @PutMapping("/{id}/status")
    @ApiOperation("更新风险状态")
    public Map<String, Object> updateRiskStatus(@PathVariable Long id, @RequestBody Map<String, Integer> request) {
        Integer status = request.get("status");
        if (status == null || (status != 0 && status != 1)) {
            Map<String, Object> result = new HashMap<>();
            result.put("code", 400);
            result.put("message", "无效的状态值，状态应为0（正常）或1（忽略）");
            return result;
        }
        
        int rows = contractRiskService.updateRiskStatus(id, status);
        
        Map<String, Object> result = new HashMap<>();
        if (rows > 0) {
            result.put("code", 200);
            result.put("message", "success");
        } else {
            result.put("code", 404);
            result.put("message", "风险不存在");
        }
        
        return result;
    }
}