package com.zioinfo.kintex.system.rolemenu;

import com.zioinfo.kintex.auth.KintexPrincipal;
import com.zioinfo.kintex.common.ApiResponse;
import com.zioinfo.kintex.common.audit.Audited;
import com.zioinfo.kintex.system.SystemAccessGuard;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/** 시스템관리 — 역할별 메뉴 접근 매핑 API. ADMIN 게이트. 기존 역할/권한(rolePerm)과 직교. */
@RestController
@RequestMapping("/api/admin/roles/{roleCode}/menus")
public class RoleMenuController {

    private final RoleMenuService service;
    private final SystemAccessGuard guard;

    public RoleMenuController(RoleMenuService service, SystemAccessGuard guard) {
        this.service = service;
        this.guard = guard;
    }

    @GetMapping
    public ApiResponse<List<RoleMenuDto>> list(@AuthenticationPrincipal KintexPrincipal principal,
                                               @PathVariable String roleCode) {
        guard.requireAdmin(principal);
        return ApiResponse.ok(service.listByRole(roleCode));
    }

    @Audited(action = "ROLE_MENU_SAVE", targetType = "sys_role_menu")
    @PostMapping
    public ApiResponse<Void> save(@AuthenticationPrincipal KintexPrincipal principal,
                                  @PathVariable String roleCode,
                                  @RequestBody RoleMenuSaveRequest req) {
        guard.requireAdmin(principal);
        service.save(roleCode, req);
        return ApiResponse.ok(null);
    }
}
