package com.CUST.brain.web.controller.system;

import com.CUST.brain.dao.domain.system.SysRole;
import com.CUST.brain.service.SysRoleService;
import com.CUST.brain.common.api.CommonResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.CUST.brain.dao.domain.system.SysPermission;
import com.CUST.brain.service.SysPermissionService;

import java.util.List;

@RestController
@RequestMapping("/api/role")
@Api(tags = "系统角色管理")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysPermissionService permissionService;

    // 新增角色
    @PostMapping("/add")
    @ApiOperation("新增角色")
    public CommonResult<?> addRole(@RequestBody SysRole role) {
        sysRoleService.addRole(role);
        return CommonResult.success(null, "新增成功");
    }

    // 更新角色
    @PutMapping("/update")
    @ApiOperation("更新角色")
    public CommonResult<?> updateRole(@RequestBody SysRole role) {
        sysRoleService.updateRole(role);
        return CommonResult.success(null, "更新成功");
    }

    // 禁用角色（逻辑删除）
    @DeleteMapping("/delete/{roleId}")
    @ApiOperation("删除角色")
    public CommonResult<?> deleteRole(@PathVariable String roleId) {
        sysRoleService.deleteRole(roleId);
        return CommonResult.success(null, "删除成功");
    }

    // 禁用角色
    @PutMapping("/disable/{roleId}")
    @ApiOperation("禁用角色")
    public CommonResult<?> disableRole(@PathVariable String roleId) {
        sysRoleService.disableRole(roleId);
        return CommonResult.success(null, "禁用成功");
    }

    // 启用角色
    @PutMapping("/restore/{roleId}")
    @ApiOperation("启用角色")
    public CommonResult<?> restoreRole(@PathVariable String roleId) {
        sysRoleService.restoreRole(roleId);
        return CommonResult.success(null, "启用成功");
    }

    @GetMapping("/detail/{roleId}")
    @ApiOperation("获取角色详情")
    @ApiParam(value = "角色ID", required = true)
    public CommonResult<SysRole> getRoleDetail(@PathVariable String roleId) {
        SysRole role = sysRoleService.getRoleDetail(roleId);
        if (role == null) {
            return CommonResult.failed("角色不存在");
        }
        return CommonResult.success(role);
    }

    @GetMapping("/page")
    @ApiOperation("分页查询角色列表")
    public CommonResult<Page<SysRole>> pageRoles(
            @ApiParam(value = "页码", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "每页数量", required = true) @RequestParam Integer pageSize,
            @ApiParam(value = "角色名称") @RequestParam(required = false) String roleName,
            @ApiParam(value = "角色编码") @RequestParam(required = false) String roleCode,
            @ApiParam(value = "状态：0-禁用，1-启用") @RequestParam(required = false) Integer status) {
        Page<SysRole> page = sysRoleService.pageRoles(pageNum, pageSize, roleName, roleCode, status);
        return CommonResult.success(page);
    }

    @GetMapping("/list")
    @ApiOperation("获取角色列表")
    public CommonResult<List<SysRole>> listRoles(
            @ApiParam(value = "角色名称（用于搜索）") @RequestParam(required = false) String roleName,
            @ApiParam(value = "状态：1-启用") @RequestParam(required = false) Integer status) {
        List<SysRole> roles = sysRoleService.listRoles(roleName, status);
        return CommonResult.success(roles);
    }
} 