package kr.iotdoor.boot.service;

import kr.iotdoor.boot.mapper.UserMapper;
import kr.iotdoor.boot.model.user.AdminUserPrincipal;
import org.springframework.context.event.EventListener;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.stereotype.Component;

@Component
public class LoginAuditService {

    private final UserMapper userMapper;

    public LoginAuditService(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @EventListener
    public void onAuthenticationSuccess(AuthenticationSuccessEvent event) {
        Object principal = event.getAuthentication().getPrincipal();
        if (principal instanceof AdminUserPrincipal adminUserPrincipal) {
            userMapper.updateLastLoginDt(adminUserPrincipal.getUserIdntfNo());
        }
    }
}
