package com.example.demo.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.model.User;
import com.example.demo.security.JwtTokenProvider;
import com.example.demo.security.UserPrincipal;
import com.example.demo.service.UserService;
import com.example.demo.web.dto.LoginRequest;
import com.example.demo.web.dto.LoginResponse;
import com.example.demo.web.dto.RegisterDto;
import com.example.demo.web.dto.RegisterResponse;

@RestController
@RequestMapping("/api")
public class AuthController {

  @Autowired
  private AuthenticationManager authenticationManager;

  @Autowired
  private UserDetailsService userDetailsService;

  @Autowired
  private JwtTokenProvider jwtTokenProvider;

  private final UserService userService;

  public AuthController(UserService userService,
      JwtTokenProvider jwtTokenProvider,
      AuthenticationManager authenticationManager) {
    this.userService = userService;
    this.jwtTokenProvider = jwtTokenProvider;
    this.authenticationManager = authenticationManager;
  }

  @PostMapping("/login")
  public ResponseEntity<?> login(@RequestBody LoginRequest body) {
    try {
      authenticationManager.authenticate(
          new UsernamePasswordAuthenticationToken(body.getUsername(), body.getPassword()));
    } catch (BadCredentialsException ex) {
      return ResponseEntity
          .status(HttpStatus.UNAUTHORIZED)
          .body(new LoginResponse("", ""));
    }

    UserPrincipal principal = (UserPrincipal) userDetailsService.loadUserByUsername(body.getUsername());
    String token = jwtTokenProvider.generateToken(principal);
    String role = principal.getRole();

    return ResponseEntity.ok(new LoginResponse(token, role));
  }

  @PostMapping("/register")
  public ResponseEntity<?> register(@RequestBody RegisterDto dto) {
    if (userService.usernameExists(dto.getUsername())) {
      return ResponseEntity
          .badRequest()
          .body("Username is already taken");
    }

    User created = userService.register(dto.getUsername(), dto.getPassword());
    return ResponseEntity
        .ok(new RegisterResponse(created.getUsername()));
  }
}