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

import cn.nianxx.thhotel.platform.access.common.enums.PlatformPermissionCode;
import cn.nianxx.thhotel.platform.common.result.PlatformPageResult;
import cn.nianxx.thhotel.platform.hotel.common.request.AdminHotelCreateRequest;
import cn.nianxx.thhotel.platform.hotel.common.request.AdminHotelStatusUpdateRequest;
import cn.nianxx.thhotel.platform.hotel.common.request.AdminHotelUpdateRequest;
import cn.nianxx.thhotel.platform.hotel.common.result.AdminHotelResult;
import cn.nianxx.thhotel.platform.hotel.service.AdminHotelManagementService;
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/hotels")
public class AdminHotelController {

    private final AdminAuthorizationService authorizationService;
    private final AdminHotelManagementService hotelManagementService;

    /**
     * 注入管理鉴权和酒店管理服务。
     */
    public AdminHotelController(
            AdminAuthorizationService authorizationService,
            AdminHotelManagementService hotelManagementService) {
        this.authorizationService = authorizationService;
        this.hotelManagementService = hotelManagementService;
    }

    /**
     * 分页查询酒店列表，需要酒店管理权限。
     */
    @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public PlatformPageResult<AdminHotelResult> listHotels(
            @RequestParam(required = false) String keyword,
            @RequestParam(name = "hotel_status", required = false) String hotelStatus,
            @RequestParam(name = "page_num", required = false) Integer pageNum,
            @RequestParam(name = "page_size", required = false) Integer pageSize) {
        authorizationService.requirePermission(PlatformPermissionCode.HOTEL_MANAGE.name());
        return hotelManagementService.queryHotels(keyword, hotelStatus, pageNum, pageSize);
    }

    /**
     * 查询酒店详情，需要酒店管理权限。
     */
    @GetMapping(value = "/{hotelId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AdminHotelResult getHotel(@PathVariable String hotelId) {
        authorizationService.requirePermission(PlatformPermissionCode.HOTEL_MANAGE.name());
        return hotelManagementService.getHotel(hotelId);
    }

    /**
     * 新增酒店，需要酒店管理权限，第一版新增后默认 DISABLED。
     */
    @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public AdminHotelResult createHotel(@RequestBody(required = false) AdminHotelCreateRequest request) {
        AuthenticatedUserContext actor = authorizationService.requirePermission(PlatformPermissionCode.HOTEL_MANAGE.name());
        return hotelManagementService.createHotel(request, actor);
    }

    /**
     * 编辑酒店基础信息，需要酒店管理权限，hotelId 不允许修改。
     */
    @PutMapping(value = "/{hotelId}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public AdminHotelResult updateHotel(
            @PathVariable String hotelId,
            @RequestBody(required = false) AdminHotelUpdateRequest request) {
        AuthenticatedUserContext actor = authorizationService.requirePermission(PlatformPermissionCode.HOTEL_MANAGE.name());
        return hotelManagementService.updateHotel(hotelId, request, actor);
    }

    /**
     * 更新酒店状态，需要酒店管理权限，启用时校验单酒店 ACTIVE 约束。
     */
    @PutMapping(value = "/{hotelId}/status", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public AdminHotelResult updateHotelStatus(
            @PathVariable String hotelId,
            @RequestBody(required = false) AdminHotelStatusUpdateRequest request) {
        AuthenticatedUserContext actor = authorizationService.requirePermission(PlatformPermissionCode.HOTEL_MANAGE.name());
        return hotelManagementService.updateHotelStatus(hotelId, request, actor);
    }
}
