package com.contract.review.controller;

import com.contract.review.entity.Regulation;
import com.contract.review.service.RegulationService;
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/regulations")
@Api(tags = "规章制度管理")
public class RegulationController {
    
    @Autowired
    private RegulationService regulationService;
    
    /**
     * 获取规章制度列表
     * @param page 页码
     * @param pageSize 每页大小
     * @param ruleNo 规则编号
     * @param ruleName 规则名称
     * @param status 状态
     * @return 规章制度列表
     */
    @GetMapping
    @ApiOperation("获取规章制度列表")
    public Map<String, Object> getRegulationList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String ruleNo,
            @RequestParam(required = false) String ruleName,
            @RequestParam(required = false) Integer status) {
        Map<String, Object> params = new HashMap<>();
        params.put("page", (page - 1) * pageSize);
        params.put("pageSize", pageSize);
        params.put("ruleNo", ruleNo);
        params.put("ruleName", ruleName);
        params.put("status", status);
        
        List<Regulation> regulationList = regulationService.getRegulationList(params);
        int total = regulationService.getRegulationCount(params);
        
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("message", "success");
        result.put("data", regulationList);
        result.put("total", total);
        result.put("page", page);
        result.put("pageSize", pageSize);
        
        return result;
    }
    
    /**
     * 根据ID获取规章制度
     * @param id 规则ID
     * @return 规章制度详情
     */
    @GetMapping("/{id}")
    @ApiOperation("获取规章制度详情")
    public Map<String, Object> getRegulationById(@PathVariable Long id) {
        Regulation regulation = regulationService.getRegulationById(id);
        
        Map<String, Object> result = new HashMap<>();
        if (regulation != null) {
            result.put("code", 200);
            result.put("message", "success");
            result.put("data", regulation);
        } else {
            result.put("code", 404);
            result.put("message", "规章制度不存在");
        }
        
        return result;
    }
    
    /**
     * 根据规则编号获取规章制度
     * @param ruleNo 规则编号
     * @return 规章制度详情
     */
    @GetMapping("/by-rule-no/{ruleNo}")
    @ApiOperation("根据规则编号获取规章制度")
    public Map<String, Object> getRegulationByRuleNo(@PathVariable String ruleNo) {
        Regulation regulation = regulationService.getRegulationByRuleNo(ruleNo);
        
        Map<String, Object> result = new HashMap<>();
        if (regulation != null) {
            result.put("code", 200);
            result.put("message", "success");
            result.put("data", regulation);
        } else {
            result.put("code", 404);
            result.put("message", "规章制度不存在");
        }
        
        return result;
    }
    
    /**
     * 新增规章制度
     * @param regulation 规章制度
     * @return 结果
     */
    @PostMapping
    @ApiOperation("新增规章制度")
    public Map<String, Object> addRegulation(@RequestBody Regulation regulation) {
        Long regulationId = regulationService.addRegulation(regulation);
        
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("message", "success");
        result.put("data", regulationId);
        
        return result;
    }
    
    /**
     * 更新规章制度
     * @param id 规则ID
     * @param regulation 规章制度
     * @return 结果
     */
    @PutMapping("/{id}")
    @ApiOperation("更新规章制度")
    public Map<String, Object> updateRegulation(@PathVariable Long id, @RequestBody Regulation regulation) {
        regulation.setId(id);
        int rows = regulationService.updateRegulation(regulation);
        
        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> deleteRegulation(@PathVariable Long id) {
        int rows = regulationService.deleteRegulation(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> updateRegulationStatus(@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 = regulationService.updateRegulationStatus(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;
    }
}