using adas_core.Authentication.Attributes;
using adas_core.Domain.Enums;
using adas_core.Infrastructure.Services;
using adas_core.Logging;
using Microsoft.AspNetCore.Mvc;

namespace adas_core.Controllers;

[Route("rabbit")]
[ApiController]
public class RabbitController(ReceiverService receiverService) : ControllerBase
{
    /// <summary>
    /// Starts the consumption of the specified error queue, processing its messages via the receiver service.
    /// Sets a unique trace identifier for the execution context to enable correlated logging of the operation.
    /// </summary>
    /// <param name="queueName">The name of the error queue to be consumed.</param>
    /// <returns>An <see cref="IActionResult"/> containing a 200 OK response when the queue consumption is initiated.</returns>
    [HttpPost]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthAdmin)]
    [Route("start-consume/{queueName}")]
    public async Task<IActionResult> ConsumeErrorQueue(string queueName)
    {
        LogExecutionContext.TrySetTraceIdentifier(Guid.NewGuid().ToString(), true);

        await receiverService.ProcessErrorQueue(queueName);
        return Ok();
    }

    /// <summary>
    /// Stops the consumption of the error queue for the specified queue name by processing it, restricted to authenticated administrators.
    /// </summary>
    /// <param name="queueName">The name of the error queue to process and stop consuming.</param>
    /// <returns>An <see cref="IActionResult"/> containing an HTTP 200 OK response once the error queue has been processed.</returns>
    [HttpPost]
    [AuthorizeRoles(PermissionEnum.RolesType.AuthAdmin)]
    [Route("stop-consume/{queueName}")]
    public async Task<IActionResult> ConsumeStopErrorQueue(string queueName)
    {
        LogExecutionContext.TrySetTraceIdentifier(Guid.NewGuid().ToString(), true);
        await receiverService.ProcessErrorQueue(queueName);
        return Ok();
    }
}