using Microsoft.AspNetCore.Mvc;

[ApiController]
[Route("api/[controller]")]
public class AuthController : ControllerBase
{
    private readonly JwtService _jwtService;
    private readonly Logger.ILogger _logger;

    public AuthController(JwtService jwtService, Logger.ILogger logger)
    {
        _jwtService = jwtService;
        _logger = logger;
    }

    [HttpPost("login")]
    public IActionResult Login(LoginRequest request)
    {
        _logger.Info($"Login attempt for email: {request.Email}");

        try
        {
            // Här ska du egentligen kolla databasen
            // men vi testar bara JWT först

            var token = _jwtService.GenerateToken(
                userId: "123",
                email: request.Email,
                role: "User"
            );

            _logger.Info($"User {request.Email} successfully logged in");
            return Ok(new { token });
        }
        catch (Exception ex)
        {
            _logger.Error($"Login failed for {request.Email}: {ex.Message}");
            return Unauthorized();
        }
    }
}