package com.example.demo.controller;

import com.example.demo.model.ApiResult;
import com.example.demo.model.DataRule;
import com.example.demo.model.Permission;
import com.example.demo.service.PermissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 权限管理控制器。
 * <p>提供基于角色的权限分配、菜单权限、按钮权限及数据权限管控接口。</p>
 */
@RestController
@RequestMapping("/api/permission")
@Tag(name = "权限管理", description = "权限管理模块接口（需求标识：SRS-F-04-001~SRS-F-04-004）")
public class PermissionController {

    private final PermissionService permissionService;

    public PermissionController(PermissionService permissionService) {
        this.permissionService = permissionService;
    }

    /**
     * 分配角色权限。
     *
     * @requirement(name="基于角色的权限分配", id="SRS-F-04-001")
     */
    @PutMapping("/role")
    @Operation(summary = "分配角色权限", description = "为指定角色分配权限ID集合")
    public ApiResult<Void> assignRolePermission(@RequestParam Long roleId,
                                                 @RequestBody List<Long> permissionIds) {
        permissionService.assignRolePermission(roleId, permissionIds);
        return ApiResult.success();
    }

    /**
     * 获取用户菜单。
     *
     * @requirement(name="菜单权限", id="SRS-F-04-002")
     */
    @GetMapping("/menu")
    @Operation(summary = "获取用户菜单", description = "根据用户角色获取可见的菜单树")
    public ApiResult<List<Permission>> getUserMenus(@RequestParam Long roleId) {
        return ApiResult.success(permissionService.getUserMenus(roleId));
    }

    /**
     * 获取用户按钮权限。
     *
     * @requirement(name="按钮权限", id="SRS-F-04-003")
     */
    @GetMapping("/button")
    @Operation(summary = "获取用户按钮权限", description = "根据用户角色获取按钮权限标识")
    public ApiResult<List<String>> getUserButtons(@RequestParam Long roleId) {
        return ApiResult.success(permissionService.getUserButtons(roleId));
    }

    /**
     * 获取数据权限规则。
     *
     * @requirement(name="数据权限分级管控", id="SRS-F-04-004")
     */
    @GetMapping("/data")
    @Operation(summary = "获取数据权限规则", description = "根据角色获取数据权限规则（本人/本部门/全部/自定义）")
    public ApiResult<DataRule> getDataRule(@RequestParam Long roleId) {
        return ApiResult.success(permissionService.getDataRule(roleId));
    }
}
