package top.tqx.demo_1.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import top.tqx.demo_1.common.Result;
import top.tqx.demo_1.dto.RoleCreateRequest;
import top.tqx.demo_1.dto.RoleUpdateRequest;
import top.tqx.demo_1.service.RoleService;
import top.tqx.demo_1.vo.PermissionVO;
import top.tqx.demo_1.vo.RoleVO;

import java.util.List;

@Tag(name = "角色管理", description = "角色和权限的管理接口")
@RestController
@RequestMapping("/role")
public class RoleController {

    private final RoleService roleService;

    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    @Operation(summary = "查询角色列表", description = "获取所有角色信息")
    @GetMapping
    public Result<List<RoleVO>> listRoles() {
        List<RoleVO> roles = roleService.listRoles();
        return Result.success(roles);
    }

    @Operation(summary = "创建角色", description = "创建新角色")
    @PostMapping
    public Result<Void> createRole(@RequestBody RoleCreateRequest request) {
        roleService.createRole(request);
        return Result.success();
    }

    @Operation(summary = "更新角色", description = "更新角色信息")
    @PutMapping("/{id}")
    public Result<Void> updateRole(
            @Parameter(description = "角色ID") @PathVariable Long id, 
            @RequestBody RoleUpdateRequest request) {
        roleService.updateRole(id, request);
        return Result.success();
    }

    @Operation(summary = "删除角色", description = "根据ID删除角色")
    @DeleteMapping("/{id}")
    public Result<Void> deleteRole(@Parameter(description = "角色ID") @PathVariable Long id) {
        roleService.deleteRole(id);
        return Result.success();
    }

    @Operation(summary = "分配权限", description = "为角色分配权限")
    @PutMapping("/{id}/permissions")
    public Result<Void> assignPermissions(
            @Parameter(description = "角色ID") @PathVariable Long id, 
            @RequestBody List<Long> permissionIds) {
        roleService.assignPermissions(id, permissionIds);
        return Result.success();
    }

    @Operation(summary = "查询角色权限", description = "获取角色的所有权限")
    @GetMapping("/{id}/permissions")
    public Result<List<PermissionVO>> getRolePermissions(
            @Parameter(description = "角色ID") @PathVariable Long id) {
        List<PermissionVO> permissions = roleService.getRolePermissions(id);
        return Result.success(permissions);
    }
}
