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 campaign management and oversight.
/// VI: API Controller Admin cho quản lý và giám sát chiến dịch.
/// </summary>
[ApiController]
[Authorize]
[Route("api/v1/admin/ads-manager/campaigns")]
[Produces("application/json")]
public class AdminCampaignsController : ControllerBase
{
    private readonly IMediator _mediator;
    private readonly ILogger<AdminCampaignsController> _logger;

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

    /// <summary>
    /// EN: List all campaigns across all advertisers (Admin only).
    /// VI: Liệt kê tất cả chiến dịch của tất cả advertisers (Admin only).
    /// </summary>
    [HttpGet]
    [ProducesResponseType(typeof(ListCampaignsResult), StatusCodes.Status200OK)]
    public async Task<ActionResult<ListCampaignsResult>> ListAllCampaigns(
        [FromQuery] string? status,
        [FromQuery] int page = 1,
        [FromQuery] int pageSize = 50)
    {
        var query = new ListCampaignsQuery
        {
            Status = status,
            Page = page,
            PageSize = pageSize
        };

        var result = await _mediator.Send(query);
        return Ok(new { success = true, data = result });
    }

    /// <summary>
    /// EN: Get campaign statistics.
    /// VI: Lấy thống kê chiến dịch.
    /// </summary>
    [HttpGet("stats")]
    [ProducesResponseType(typeof(CampaignStatsDto), StatusCodes.Status200OK)]
    public async Task<ActionResult<CampaignStatsDto>> GetCampaignStats()
    {
        var stats = await _mediator.Send(new GetCampaignStatsQuery());
        return Ok(new { success = true, data = stats });
    }
}

/// <summary>
/// EN: Campaign statistics DTO.
/// VI: DTO thống kê chiến dịch.
/// </summary>
public record CampaignStatsDto
{
    public int TotalCampaigns { get; init; }
    public int ActiveCampaigns { get; init; }
    public int PausedCampaigns { get; init; }
    public int DraftCampaigns { get; init; }
    public int CompletedCampaigns { get; init; }
    public decimal TotalSpend { get; init; }
    public decimal TotalBudget { get; init; }
}

/// <summary>
/// EN: Query to get campaign statistics.
/// VI: Query lấy thống kê chiến dịch.
/// </summary>
public record GetCampaignStatsQuery : IRequest<CampaignStatsDto>;
