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

import cn.nianxx.thhotel.platform.access.common.enums.PlatformPermissionCode;
import cn.nianxx.thhotel.platform.common.result.PlatformPageResult;
import cn.nianxx.thhotel.platform.navigation.common.request.AdminMenuCreateRequest;
import cn.nianxx.thhotel.platform.navigation.common.request.AdminMenuUpdateRequest;
import cn.nianxx.thhotel.platform.navigation.common.result.AdminMenuResult;
import cn.nianxx.thhotel.platform.navigation.service.AdminMenuManagementService;
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/menus")
public class AdminMenuController {

    private final AdminAuthorizationService authorizationService;
    private final AdminMenuManagementService menuManagementService;

    /**
     * 注入管理鉴权和菜单管理服务。
     */
    public AdminMenuController(
            AdminAuthorizationService authorizationService,
            AdminMenuManagementService menuManagementService) {
        this.authorizationService = authorizationService;
        this.menuManagementService = menuManagementService;
    }

    /**
     * 分页查询菜单列表，需要菜单管理权限。
     */
    @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public PlatformPageResult<AdminMenuResult> listMenus(
            @RequestParam(required = false) String keyword,
            @RequestParam(name = "menu_status", required = false) String menuStatus,
            @RequestParam(name = "page_num", required = false) Integer pageNum,
            @RequestParam(name = "page_size", required = false) Integer pageSize) {
        authorizationService.requirePermission(PlatformPermissionCode.SYSTEM_MENU_MANAGE.name());
        return menuManagementService.queryMenus(keyword, menuStatus, pageNum, pageSize);
    }

    /**
     * 查询菜单详情，需要菜单管理权限。
     */
    @GetMapping(value = "/{menuId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AdminMenuResult getMenu(@PathVariable String menuId) {
        authorizationService.requirePermission(PlatformPermissionCode.SYSTEM_MENU_MANAGE.name());
        return menuManagementService.getMenu(menuId);
    }

    /**
     * 新增菜单配置，需要菜单管理权限。
     */
    @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public AdminMenuResult createMenu(@RequestBody(required = false) AdminMenuCreateRequest request) {
        AuthenticatedUserContext actor = authorizationService.requirePermission(
                PlatformPermissionCode.SYSTEM_MENU_MANAGE.name());
        return menuManagementService.createMenu(request, actor);
    }

    /**
     * 编辑菜单配置，需要菜单管理权限，菜单代码不允许修改。
     */
    @PutMapping(value = "/{menuId}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public AdminMenuResult updateMenu(
            @PathVariable String menuId,
            @RequestBody(required = false) AdminMenuUpdateRequest request) {
        AuthenticatedUserContext actor = authorizationService.requirePermission(
                PlatformPermissionCode.SYSTEM_MENU_MANAGE.name());
        return menuManagementService.updateMenu(menuId, request, actor);
    }
}
