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

import cn.nianxx.thhotel.platform.access.common.enums.PlatformPermissionCode;
import cn.nianxx.thhotel.platform.common.result.PlatformPageResult;
import cn.nianxx.thhotel.platform.identity.common.request.AdminUserCreateRequest;
import cn.nianxx.thhotel.platform.identity.common.request.AdminUserHotelAssignmentRequest;
import cn.nianxx.thhotel.platform.identity.common.request.AdminUserPasswordResetRequest;
import cn.nianxx.thhotel.platform.identity.common.request.AdminUserRoleAssignmentRequest;
import cn.nianxx.thhotel.platform.identity.common.request.AdminUserUpdateRequest;
import cn.nianxx.thhotel.platform.identity.common.result.AdminUserDetailResult;
import cn.nianxx.thhotel.platform.identity.common.result.AdminUserListItemResult;
import cn.nianxx.thhotel.platform.identity.common.result.AdminUserPasswordResetResult;
import cn.nianxx.thhotel.platform.identity.service.AdminUserManagementService;
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/users")
public class AdminUserController {

    private final AdminAuthorizationService authorizationService;
    private final AdminUserManagementService userManagementService;

    /**
     * 注入管理鉴权和用户管理服务。
     */
    public AdminUserController(
            AdminAuthorizationService authorizationService,
            AdminUserManagementService userManagementService) {
        this.authorizationService = authorizationService;
        this.userManagementService = userManagementService;
    }

    /**
     * 分页查询用户列表，需要用户管理权限。
     */
    @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public PlatformPageResult<AdminUserListItemResult> listUsers(
            @RequestParam(required = false) String keyword,
            @RequestParam(name = "user_status", required = false) String userStatus,
            @RequestParam(name = "page_num", required = false) Integer pageNum,
            @RequestParam(name = "page_size", required = false) Integer pageSize) {
        authorizationService.requirePermission(PlatformPermissionCode.SYSTEM_USER_MANAGE.name());
        return userManagementService.queryUsers(keyword, userStatus, pageNum, pageSize);
    }

    /**
     * 查询单个用户详情，需要用户管理权限。
     */
    @GetMapping(value = "/{userId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AdminUserDetailResult getUser(@PathVariable String userId) {
        authorizationService.requirePermission(PlatformPermissionCode.SYSTEM_USER_MANAGE.name());
        return userManagementService.getUser(userId);
    }

    /**
     * 新增普通用户，需要用户管理权限。
     */
    @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public AdminUserDetailResult createUser(@RequestBody(required = false) AdminUserCreateRequest request) {
        AuthenticatedUserContext actor = authorizationService.requirePermission(
                PlatformPermissionCode.SYSTEM_USER_MANAGE.name());
        return userManagementService.createUser(request, actor);
    }

    /**
     * 编辑用户基础信息和状态，需要用户管理权限。
     */
    @PutMapping(value = "/{userId}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public AdminUserDetailResult updateUser(
            @PathVariable String userId,
            @RequestBody(required = false) AdminUserUpdateRequest request) {
        AuthenticatedUserContext actor = authorizationService.requirePermission(
                PlatformPermissionCode.SYSTEM_USER_MANAGE.name());
        return userManagementService.updateUser(userId, request, actor);
    }

    /**
     * 覆盖用户角色，需要用户管理权限。
     */
    @PutMapping(value = "/{userId}/roles", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public AdminUserDetailResult assignRoles(
            @PathVariable String userId,
            @RequestBody(required = false) AdminUserRoleAssignmentRequest request) {
        AuthenticatedUserContext actor = authorizationService.requirePermission(
                PlatformPermissionCode.SYSTEM_USER_MANAGE.name());
        return userManagementService.assignRoles(userId, request, actor);
    }

    /**
     * 覆盖用户酒店授权，需要用户管理权限。
     */
    @PutMapping(value = "/{userId}/hotels", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public AdminUserDetailResult assignHotels(
            @PathVariable String userId,
            @RequestBody(required = false) AdminUserHotelAssignmentRequest request) {
        AuthenticatedUserContext actor = authorizationService.requirePermission(
                PlatformPermissionCode.SYSTEM_USER_MANAGE.name());
        return userManagementService.assignHotels(userId, request, actor);
    }

    /**
     * 重置用户密码，需要用户管理权限，临时密码只在本次响应返回。
     */
    @PostMapping(value = "/{userId}/password-reset", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public AdminUserPasswordResetResult resetPassword(
            @PathVariable String userId,
            @RequestBody(required = false) AdminUserPasswordResetRequest request) {
        AuthenticatedUserContext actor = authorizationService.requirePermission(
                PlatformPermissionCode.SYSTEM_USER_MANAGE.name());
        return userManagementService.resetPassword(userId, request, actor);
    }
}
