package org.ta.pddserver.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;
import org.ta.pddserver.config.security.JwtUtils;
import org.ta.pddserver.config.security.RoleUserDetails;
import org.ta.pddserver.config.security.UserDetailsServiceImpl;
import org.ta.pddserver.model.api.request.UserRequest;
import org.ta.pddserver.utils.HttpRequestHandlerTool;
import org.ta.pddserver.utils.JsonResponse;

@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
public class AuthController {
    private final AuthenticationManager authenticationManager;
    private final JwtUtils jwtUtils;

    @Resource
    UserDetailsServiceImpl userDetailsService;

    @PostMapping("/login")
    public JSONObject login(HttpServletRequest request, @RequestBody UserRequest loginRequest) {
        Authentication authentication = authenticationManager.authenticate(
                new UsernamePasswordAuthenticationToken(
                        loginRequest.getAccount(),
                        loginRequest.getPassword()
                )
        );
        SecurityContextHolder.getContext().setAuthentication(authentication);
        RoleUserDetails userDetails = (RoleUserDetails) authentication.getPrincipal();
        userDetails.setLoginIp(HttpRequestHandlerTool.getClientIP(request));
        String jwt = jwtUtils.generateToken(userDetails);
        JSONObject response = new JSONObject();
        response.put("token", jwt);
        response.put("username", userDetails.getUsername());
        response.put("role", userDetails.getRole());
        response.put("authorities", userDetails.getAuthorities());
        return JsonResponse.generateLocalCommonSuccessResponse(response, null);
    }

    @PostMapping("/logout")
    public JSONObject logout(@RequestBody UserRequest loginRequest) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        userDetailsService.removeUser(username);
        return JsonResponse.generateLocalCommonSuccessResponse(true, null);
    }

    @PostMapping("/checkUser")
    public JSONObject checkUser(@RequestBody UserRequest loginRequest) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        RoleUserDetails userDetails = (RoleUserDetails) authentication.getPrincipal();
        return JsonResponse.generateLocalCommonSuccessResponse(userDetails, null);
    }
}
