package com.samsung.interceptor;

import com.samsung.common.util.WebUtil;
import com.samsung.user.UserService;
import com.samsung.user.entity.User;
import com.samsung.user.interceptor.LoginInterceptor;
import lombok.extern.log4j.Log4j2;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;

import jakarta.servlet.http.HttpServletResponse;
import java.util.Date;

@Log4j2
public class UserUpdateInterceptor implements LoginInterceptor {

    @Autowired
    private UserService userService;

    @Autowired
    private WebUtil webUtil;

    @Override
    public void postLogin(HttpServletRequest request, HttpServletResponse response, User user) {

        String recentLoginIp = user.getRecentLoginIp();
        Date recentLoginDatetime = user.getRecentLoginDatetime();
        user.setLastLoginDate(recentLoginDatetime);
        user.setLastLoginIp(recentLoginIp);
        user.setRecentLoginDatetime(new Date());
        user.setRecentLoginIp(webUtil.getClientIp(request));
        user.setLastActivityTime(System.currentTimeMillis());
        userService.updateUser(user);
    }
}
