using AdsServingService.API.Application.Queries;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace AdsServingService.API.Controllers;

/// <summary>
/// EN: Admin API Controller for managing and monitoring auctions.
/// VI: API Controller Admin để quản lý và giám sát các phiên đấu giá.
/// </summary>
[ApiController]
[Authorize]
[Route("api/v1/admin/auctions")]
[Produces("application/json")]
public class AdminAuctionsController : ControllerBase
{
    private readonly IMediator _mediator;
    private readonly ILogger<AdminAuctionsController> _logger;

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

    /// <summary>
    /// EN: Get paginated list of auctions with optional filters.
    /// VI: Lấy danh sách auctions phân trang với bộ lọc tùy chọn.
    /// </summary>
    [HttpGet]
    [ProducesResponseType(typeof(PagedResult<AuctionDto>), StatusCodes.Status200OK)]
    public async Task<ActionResult<PagedResult<AuctionDto>>> GetAuctions(
        [FromQuery] Guid? userId,
        [FromQuery] string? placementType,
        [FromQuery] DateTime? startDate,
        [FromQuery] DateTime? endDate,
        [FromQuery] int page = 1,
        [FromQuery] int pageSize = 20)
    {
        var query = new GetAuctionsQuery
        {
            UserId = userId,
            PlacementType = placementType,
            StartDate = startDate,
            EndDate = endDate,
            Page = page,
            PageSize = pageSize
        };

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

    /// <summary>
    /// EN: Get auction statistics (win rates, average eCPM, etc.).
    /// VI: Lấy thống kê đấu giá (tỷ lệ thắng, eCPM trung bình, v.v.).
    /// </summary>
    [HttpGet("statistics")]
    [ProducesResponseType(typeof(AuctionStatisticsDto), StatusCodes.Status200OK)]
    public async Task<ActionResult<AuctionStatisticsDto>> GetStatistics()
    {
        _logger.LogInformation("Auction statistics requested");
        var statistics = await _mediator.Send(new GetAuctionStatisticsQuery());
        return Ok(new { success = true, data = statistics });
    }
}
