package com.realnet.users.controller;

import com.realnet.users.entity.User;
import com.realnet.users.model.JwtResponse;
import com.realnet.users.model.LoginRequest;
import com.realnet.users.model.RegisterRequest;
import com.realnet.users.service.AuthService;
import com.realnet.users.util.JwtUtil; // Assuming this will be created in 1.6
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid; // Assuming javax.validation is available

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

    @Autowired
    private AuthService authService;

    @Autowired
    private JwtUtil jwtUtil; // Assuming this will be created in 1.6

    @PostMapping("/register")
    public ResponseEntity<?> registerUser(@Valid @RequestBody RegisterRequest registerRequest) {
        User registeredUser = authService.registerUser(registerRequest);
        return ResponseEntity.ok("User registered successfully!");
    }

    @PostMapping("/login")
    public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginRequest loginRequest) {
        String jwt = authService.authenticateUserAndGenerateJwt(loginRequest);
        // In a real application, you would fetch user details to populate JwtResponse
        // For simplicity, let's assume we can get username from loginRequest for now
        // and ID/email would be fetched from DB after authentication
        // This part needs refinement with actual UserDetails implementation
        return ResponseEntity.ok(new JwtResponse(jwt, null, loginRequest.getUsername(), null));
    }
}