package com.zioinfo.hrm.uiws.system.controller;

import com.zioinfo.hrm.common.ApiResponse;
import com.zioinfo.hrm.uiws.system.dto.PageResponse;
import com.zioinfo.hrm.uiws.system.dto.RoleDto;
import com.zioinfo.hrm.uiws.system.dto.RoleMenuDto;
import com.zioinfo.hrm.uiws.system.dto.RoleMenuSaveRequest;
import com.zioinfo.hrm.uiws.system.service.RoleMenuService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/** 2.5 메뉴생성(role-menus) — 권한목록 / 권한별 메뉴매핑 조회·저장. */
@RestController
@RequiredArgsConstructor
public class RoleMenuController {

    private final RoleMenuService roleMenuService;

    @GetMapping("/api/system/role-menus")
    public ApiResponse<PageResponse<RoleDto>> listRoles(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "20") int size) {
        return ApiResponse.ok(roleMenuService.listRoles(page, size));
    }

    @GetMapping("/api/system/roles/{roleId}/menus")
    public ApiResponse<List<RoleMenuDto>> getRoleMenus(@PathVariable("roleId") String roleId) {
        return ApiResponse.ok(roleMenuService.getRoleMenus(roleId));
    }

    @PutMapping("/api/system/roles/{roleId}/menus")
    public ApiResponse<Void> saveRoleMenus(@PathVariable("roleId") String roleId, @Valid @RequestBody RoleMenuSaveRequest req) {
        roleMenuService.saveRoleMenus(roleId, req.menus());
        return ApiResponse.ok(null);
    }
}
