package com.ga.common.menu;

import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class PermissionChecker {

    private final MenuMapper menuMapper;

    /** 사용자 + 메뉴 + 권한 조합을 체크. 캐시: userPerm::userId_menuCode_permCode */
    @Cacheable(value = "userPerm",
            key = "T(java.lang.String).format('%d_%s_%s', #userId, #menuCode, #permCode)",
            unless = "#result == false")
    public boolean hasPermission(Long userId, String menuCode, String permCode) {
        if (userId == null) return false;
        return menuMapper.hasPermission(userId, menuCode, permCode) > 0;
    }
}
