using AdsManagerService.API.Application.Commands;
using AdsManagerService.API.Application.Queries;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace AdsManagerService.API.Controllers;

/// <summary>
/// EN: Admin API Controller for ad review and moderation.
/// VI: API Controller Admin cho duyệt và kiểm duyệt quảng cáo.
/// </summary>
[ApiController]
[Authorize]
[Route("api/v1/admin/ads-manager/ads")]
[Produces("application/json")]
public class AdminAdsController : ControllerBase
{
    private readonly IMediator _mediator;
    private readonly ILogger<AdminAdsController> _logger;

    public AdminAdsController(IMediator mediator, ILogger<AdminAdsController> logger)
    {
        _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));
    }

    /// <summary>
    /// EN: List pending ads for review.
    /// VI: Liệt kê quảng cáo chờ duyệt.
    /// </summary>
    [HttpGet("pending")]
    [ProducesResponseType(typeof(List<AdDto>), StatusCodes.Status200OK)]
    public async Task<ActionResult<List<AdDto>>> ListPendingAds(
        [FromQuery] int page = 1,
        [FromQuery] int pageSize = 20)
    {
        var ads = await _mediator.Send(new ListPendingAdsQuery
        {
            Page = page,
            PageSize = pageSize
        });

        return Ok(new { success = true, data = ads });
    }

    /// <summary>
    /// EN: Approve an ad.
    /// VI: Phê duyệt quảng cáo.
    /// </summary>
    [HttpPost("{id}/approve")]
    [ProducesResponseType(StatusCodes.Status204NoContent)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> ApproveAd(Guid id)
    {
        _logger.LogInformation("Approving ad {AdId}", id);

        var result = await _mediator.Send(new ApproveAdCommand { AdId = id });

        if (!result)
            return NotFound(new { success = false, error = new { code = "NOT_FOUND", message = "Ad not found" } });

        return NoContent();
    }

    /// <summary>
    /// EN: Reject an ad.
    /// VI: Từ chối quảng cáo.
    /// </summary>
    [HttpPost("{id}/reject")]
    [ProducesResponseType(StatusCodes.Status204NoContent)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> RejectAd(Guid id, [FromBody] RejectAdRequest request)
    {
        _logger.LogInformation("Rejecting ad {AdId}", id);

        var result = await _mediator.Send(new RejectAdCommand
        {
            AdId = id,
            Reason = request.Reason
        });

        if (!result)
            return NotFound(new { success = false, error = new { code = "NOT_FOUND", message = "Ad not found" } });

        return NoContent();
    }
}

/// <summary>
/// EN: Request model for rejecting ad.
/// VI: Request model từ chối quảng cáo.
/// </summary>
public record RejectAdRequest
{
    public string Reason { get; init; } = null!;
}

/// <summary>
/// EN: Query to list pending ads.
/// VI: Query liệt kê quảng cáo chờ duyệt.
/// </summary>
public record ListPendingAdsQuery : IRequest<List<AdDto>>
{
    public int Page { get; init; } = 1;
    public int PageSize { get; init; } = 20;
}

/// <summary>
/// EN: Command to approve ad.
/// VI: Command phê duyệt quảng cáo.
/// </summary>
public record ApproveAdCommand : IRequest<bool>
{
    public Guid AdId { get; init; }
}

/// <summary>
/// EN: Command to reject ad.
/// VI: Command từ chối quảng cáo.
/// </summary>
public record RejectAdCommand : IRequest<bool>
{
    public Guid AdId { get; init; }
    public string Reason { get; init; } = null!;
}
