package cn.nianxx.thhotel.platform.access.control;

import cn.nianxx.thhotel.platform.access.common.enums.PlatformPermissionCode;
import cn.nianxx.thhotel.platform.access.common.result.AdminPermissionResult;
import cn.nianxx.thhotel.platform.access.service.AdminAccessManagementService;
import cn.nianxx.thhotel.platform.security.service.AdminAuthorizationService;
import java.util.List;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 管理后台权限 Controller。权限码第一版只读，不允许运营随意新增。
 */
@RestController
@RequestMapping("/api/admin/permissions")
public class AdminPermissionController {

    private final AdminAuthorizationService authorizationService;
    private final AdminAccessManagementService accessManagementService;

    /**
     * 注入管理鉴权和角色权限管理服务。
     */
    public AdminPermissionController(
            AdminAuthorizationService authorizationService,
            AdminAccessManagementService accessManagementService) {
        this.authorizationService = authorizationService;
        this.accessManagementService = accessManagementService;
    }

    /**
     * 查询全部权限码，需要角色管理权限。
     */
    @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public List<AdminPermissionResult> listPermissions() {
        authorizationService.requirePermission(PlatformPermissionCode.SYSTEM_ROLE_MANAGE.name());
        return accessManagementService.listPermissions();
    }
}
