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

import cn.nianxx.thhotel.platform.access.common.enums.PlatformPermissionCode;
import cn.nianxx.thhotel.platform.access.common.request.AdminRoleCreateRequest;
import cn.nianxx.thhotel.platform.access.common.request.AdminRolePermissionAssignmentRequest;
import cn.nianxx.thhotel.platform.access.common.request.AdminRoleUpdateRequest;
import cn.nianxx.thhotel.platform.access.common.result.AdminRoleDetailResult;
import cn.nianxx.thhotel.platform.access.common.result.AdminRoleListItemResult;
import cn.nianxx.thhotel.platform.access.service.AdminAccessManagementService;
import cn.nianxx.thhotel.platform.common.result.PlatformPageResult;
import cn.nianxx.thhotel.platform.security.common.dto.AuthenticatedUserContext;
import cn.nianxx.thhotel.platform.security.service.AdminAuthorizationService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 管理后台角色 Controller。提供角色只读查询入口。
 */
@RestController
@RequestMapping("/api/admin/roles")
public class AdminRoleController {

    private final AdminAuthorizationService authorizationService;
    private final AdminAccessManagementService accessManagementService;

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

    /**
     * 分页查询角色列表，需要角色管理权限。
     */
    @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public PlatformPageResult<AdminRoleListItemResult> listRoles(
            @RequestParam(required = false) String keyword,
            @RequestParam(name = "role_status", required = false) String roleStatus,
            @RequestParam(name = "page_num", required = false) Integer pageNum,
            @RequestParam(name = "page_size", required = false) Integer pageSize) {
        authorizationService.requirePermission(PlatformPermissionCode.SYSTEM_ROLE_MANAGE.name());
        return accessManagementService.queryRoles(keyword, roleStatus, pageNum, pageSize);
    }

    /**
     * 查询角色详情，需要角色管理权限。
     */
    @GetMapping(value = "/{roleId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AdminRoleDetailResult getRole(@PathVariable String roleId) {
        authorizationService.requirePermission(PlatformPermissionCode.SYSTEM_ROLE_MANAGE.name());
        return accessManagementService.getRole(roleId);
    }

    /**
     * 新增自定义角色，需要角色管理权限。
     */
    @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public AdminRoleDetailResult createRole(@RequestBody(required = false) AdminRoleCreateRequest request) {
        AuthenticatedUserContext actor = authorizationService.requirePermission(
                PlatformPermissionCode.SYSTEM_ROLE_MANAGE.name());
        return accessManagementService.createRole(request, actor);
    }

    /**
     * 编辑自定义角色，需要角色管理权限；内置角色会被服务层拒绝。
     */
    @PutMapping(value = "/{roleId}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public AdminRoleDetailResult updateRole(
            @PathVariable String roleId,
            @RequestBody(required = false) AdminRoleUpdateRequest request) {
        AuthenticatedUserContext actor = authorizationService.requirePermission(
                PlatformPermissionCode.SYSTEM_ROLE_MANAGE.name());
        return accessManagementService.updateRole(roleId, request, actor);
    }

    /**
     * 覆盖自定义角色权限，需要角色管理权限。
     */
    @PutMapping(value = "/{roleId}/permissions", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public AdminRoleDetailResult assignPermissions(
            @PathVariable String roleId,
            @RequestBody(required = false) AdminRolePermissionAssignmentRequest request) {
        AuthenticatedUserContext actor = authorizationService.requirePermission(
                PlatformPermissionCode.SYSTEM_ROLE_MANAGE.name());
        return accessManagementService.assignPermissions(roleId, request, actor);
    }
}
