package kr.co.accura.wbx.spring.rbac;

import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import org.springframework.data.jpa.domain.Specification;

/**
 * WBX 데이터 접근 범위
 * OWN = 본인 데이터만
 * DEPT = 소속 부서 데이터
 * COMPANY = 전사 데이터
 */
public enum DeptScope {
    OWN,
    DEPT,
    COMPANY;

    /**
     * JPA Specification 자동 생성
     */
    public <T> Specification<T> toSpec(Long userId, Long deptId, String userField, String deptField) {
        return switch (this) {
            case OWN -> (root, query, cb) -> cb.equal(root.get(userField), userId);
            case DEPT -> (root, query, cb) -> cb.equal(root.get(deptField), deptId);
            case COMPANY -> (root, query, cb) -> cb.conjunction(); // no filter
        };
    }
}
