package com.samsung.user.controller;

import com.samsung.authentication.Account;
import com.samsung.common.entity.JwtResponse;
import com.samsung.common.util.JwtUtil;
import com.samsung.common.util.WebUtil;
import com.samsung.user.AuthenticationService;
import com.samsung.user.entity.User;
import com.samsung.user.interceptor.LoginInterceptorExecutor;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.log4j.Log4j2;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Log4j2
@RestController
public class LoginController {

    private final AuthenticationService authenticationService;

    private final LoginInterceptorExecutor loginInterceptorExecutor;

    private final WebUtil webUtil;

    private final JwtUtil jwtUtil;

    public LoginController(AuthenticationService authenticationService, LoginInterceptorExecutor loginInterceptorExecutor, WebUtil webUtil, JwtUtil jwtUtil) {
        this.authenticationService = authenticationService;
        this.loginInterceptorExecutor = loginInterceptorExecutor;
        this.webUtil = webUtil;
        this.jwtUtil = jwtUtil;
    }

    @PostMapping("/noauth/login/id")
    @Operation(summary = "ID/PW 로그인")
    public JwtResponse loginById(HttpServletRequest request, HttpServletResponse response,
                                 @RequestParam(value = "userId") String userId,
                                 @RequestParam(value = "password") String password) {
        loginInterceptorExecutor.applyPreLogin(request, response);
        User user = authenticationService.loginById(userId, password);
        user.setRecentLoginIp(webUtil.getClientIp(request));
        JwtResponse jwtResponse = new JwtResponse(jwtUtil.generateToken(user), userId);
        user.setJwt(jwtResponse.getJwtToken().split("\\.")[2]);
        loginInterceptorExecutor.applyPostLogin(request, response, user);
        return jwtResponse;
    }

    @PostMapping("/noauth/login/new-eptray")
    @Operation(summary = "knox new eptray 로그인")
    public JwtResponse loginByNewEptray(HttpServletRequest request, HttpServletResponse response, @RequestParam String encodeUserInfo, @RequestParam String encodeAesKey ) {
        loginInterceptorExecutor.applyPreLogin(request, response);
        User user = authenticationService.loginByNewEpTray(encodeUserInfo, encodeAesKey);
        user.setRecentLoginIp(webUtil.getClientIp(request));
        JwtResponse jwtResponse = new JwtResponse(jwtUtil.generateToken(user), user.getUserId());
        user.setJwt(jwtResponse.getJwtToken().split("\\.")[2]);
        loginInterceptorExecutor.applyPostLogin(request, response, user);
        return jwtResponse;
    }

    @PutMapping("/auth/logout")
    @Operation(summary = "로그 아웃")
    public ResponseEntity<Object> logout(HttpServletRequest request, HttpServletResponse response) {
        loginInterceptorExecutor.applyPreLogout(request, response, Account.currentUser());
        authenticationService.logout();
        loginInterceptorExecutor.applyPostLogout(request, response, Account.currentUser());
        return ResponseEntity.ok().build();
    }
}
