package com.kamco.cd.kamcoback.auth;

import com.kamco.cd.kamcoback.members.service.AuthService;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class AuthFailureEventListener implements ApplicationListener<AbstractAuthenticationFailureEvent> {

  private final AuthService authService;

  @Override
  public void onApplicationEvent(AbstractAuthenticationFailureEvent event) {
    // 로그인 시도에 사용된 (username)
    Object principal = event.getAuthentication().getPrincipal();

    if (principal instanceof String username) {
      // 로그인 실패 카운트 증가 로직 호출
      authService.loginFail(UUID.fromString(username));
    }
  }

  @Override
  public boolean supportsAsyncExecution() {
    return ApplicationListener.super.supportsAsyncExecution();
  }
}
