package dev.ksan.etfoglasiserver.controller;

import dev.ksan.etfoglasiserver.dto.JwtResponseDTO;
import dev.ksan.etfoglasiserver.dto.UserCreationDTO;
import dev.ksan.etfoglasiserver.dto.UserDTO;
import dev.ksan.etfoglasiserver.dto.UserLoginDTO;
import dev.ksan.etfoglasiserver.model.NotificationMethod;
import dev.ksan.etfoglasiserver.model.User;
import dev.ksan.etfoglasiserver.service.UserService;

import java.util.Map;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

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

  @Autowired UserService service;


  @GetMapping("/users/me")
  public ResponseEntity<UserDTO> getUserById(Authentication authentication) {
    String email = authentication.getName();

    User user = service.getAccountByEmail(email)
            .orElseThrow(() -> new ResponseStatusException(
                    HttpStatus.NOT_FOUND, "User not found"
            ));

    return ResponseEntity.ok(new UserDTO(user));
  }

  @PutMapping("/users/me")
  public ResponseEntity<UserDTO> updateUser(
          @RequestBody UserCreationDTO user,
          Authentication authentication) {

    String email = authentication.getName();

    UserDTO userUpdated = service.updateUser(email, user);

    return ResponseEntity.ok(userUpdated);
  }



  @PostMapping("/users/me/notification-type")
  public ResponseEntity<?> updateNotificationType(
          @RequestBody Map<String, String> request,
          Authentication authentication) {

    String email = authentication.getName();
    NotificationMethod type = NotificationMethod.valueOf(request.get("notificationType"));
    service.updateNotificationType(email, type);

    return ResponseEntity.ok(Map.of("status", "updated"));
  }

  @PutMapping("/users/me/subjects/{subjectId}")
  public ResponseEntity<UserDTO> addSubject(
          @PathVariable UUID subjectId,
          Authentication authentication) {

    String email = authentication.getName();

    UserDTO updatedUser = service.addSubject(email, subjectId);

    return ResponseEntity.ok(updatedUser);
  }

  @DeleteMapping("/users/me/subjects/{subjectId}")
  public ResponseEntity<UserDTO> removeSubject(
          @PathVariable UUID subjectId,
          Authentication authentication) {

    String email = authentication.getName();

    UserDTO updatedUser = service.removeSubject(email, subjectId);

    return ResponseEntity.ok(updatedUser);
  }

  // not the best, should fix this later
  @PostMapping("/login")
  public ResponseEntity<JwtResponseDTO> login(@RequestBody UserLoginDTO user) {
    try {
      JwtResponseDTO userVer = service.verify(user);

      if (userVer != null) return new ResponseEntity<>(userVer, HttpStatus.OK);
    }catch (Exception e) {

      return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }

    return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
  }

  // not the best, should fix this later too
  @PostMapping("/register")
  public ResponseEntity<UserDTO> register(@RequestBody UserCreationDTO user) {

    try {
      UserDTO newUser = service.register(user);
      if (newUser != null) return new ResponseEntity<>(newUser, HttpStatus.CREATED);
    }catch (Exception e) {

      return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }
    return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
  }



  @DeleteMapping("/users/me")
  public ResponseEntity<Void> deleteUser(Authentication authentication) {

    String email = authentication.getName();

    service.deleteUser(email);

    return ResponseEntity.noContent().build();
  }
}
