using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.RateLimiting;
using Meezi.API.Models.Tables;
using Meezi.API.Services;
using Meezi.Shared;

namespace Meezi.API.Controllers;

[ApiController]
[AllowAnonymous]
[Route("api/q")]
public class QrController : ControllerBase
{
    private readonly ITableService _tableService;

    public QrController(ITableService tableService)
    {
        _tableService = tableService;
    }

    [HttpGet("{qrCode}")]
    [EnableRateLimiting("public-read")]
    public async Task<IActionResult> Resolve(string qrCode, CancellationToken cancellationToken)
    {
        var data = await _tableService.ResolveQrAsync(qrCode, cancellationToken);
        if (data is null)
            return NotFound(new ApiResponse<object>(false, null, new ApiError("NOT_FOUND", "QR code not found.")));

        return Ok(new ApiResponse<QrResolveResponse>(true, data));
    }
}
