package com.bio.bio_backend.global.config;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "security")
public class SecurityPathConfig {
    
    private List<String> permitAllPaths = new ArrayList<>();
    
    /**
     * 주어진 경로가 허용된 경로인지 확인
     * @param path 확인할 경로
     * @param contextPath 컨텍스트 경로
     * @return 허용 여부
     */
    public boolean isPermittedPath(String path, String contextPath) {
        if (permitAllPaths == null) {
            return false;
        }
        
        return permitAllPaths.stream()
                .anyMatch(permittedPath -> {
                    String fullPath = contextPath + permittedPath;
                    if (permittedPath.endsWith("/**")) {
                        // /** 패턴 처리
                        String basePath = fullPath.substring(0, fullPath.length() - 2);
                        return path.startsWith(basePath);
                    } else {
                        return path.equals(fullPath);
                    }
                });
    }
}
