package com.ga.api.support;

import com.ga.common.auth.LoginUser;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors;
import org.springframework.test.web.servlet.request.RequestPostProcessor;

import java.util.List;

/**
 * 테스트에서 LoginUser 를 SecurityContext 에 직접 주입하는 헬퍼.
 *
 * 사용 예:
 *   mockMvc.perform(get("/api/agents").with(MockLoginUser.admin()))
 */
public final class MockLoginUser {

    private MockLoginUser() {}

    /** userId=1, roleCode=SUPER_ADMIN 인 관리자 사용자 */
    public static RequestPostProcessor admin() {
        return asUser(1L, "admin", List.of("SUPER_ADMIN"));
    }

    public static RequestPostProcessor asUser(Long userId, String loginId, List<String> roles) {
        LoginUser loginUser = LoginUser.builder()
                .userId(userId)
                .loginId(loginId)
                .userName("테스트사용자")
                .roleCodes(roles)
                .build();
        Authentication auth = new UsernamePasswordAuthenticationToken(
                loginUser, null, loginUser.getAuthorities());
        return SecurityMockMvcRequestPostProcessors.authentication(auth);
    }
}
