using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PromotionService.API.Application.DTOs;
using PromotionService.API.Application.Queries;

namespace PromotionService.API.Controllers.Admin;

/// <summary>
/// EN: Admin controller for Redemption management.
/// VI: Admin controller để quản lý Redemption.
/// </summary>
[ApiController]
[Route("api/v1/admin/redemptions")]
[Authorize]
[Produces("application/json")]
public class AdminRedemptionsController : ControllerBase
{
    private readonly IMediator _mediator;
    private readonly ILogger<AdminRedemptionsController> _logger;

    public AdminRedemptionsController(IMediator mediator, ILogger<AdminRedemptionsController> logger)
    {
        _mediator = mediator;
        _logger = logger;
    }

    /// <summary>
    /// EN: Get all redemptions with pagination and filters.
    /// VI: Lấy tất cả redemptions với phân trang và bộ lọc.
    /// </summary>
    [HttpGet]
    [ProducesResponseType(typeof(PaginatedResponse<AdminRedemptionListDto>), StatusCodes.Status200OK)]
    public async Task<ActionResult<PaginatedResponse<AdminRedemptionListDto>>> GetAllRedemptions(
        [FromQuery] int pageNumber = 1,
        [FromQuery] int pageSize = 20,
        [FromQuery] Guid? campaignId = null,
        [FromQuery] Guid? voucherId = null,
        [FromQuery] Guid? userId = null,
        [FromQuery] DateTime? dateFrom = null,
        [FromQuery] DateTime? dateTo = null)
    {
        var result = await _mediator.Send(new GetAllRedemptionsQuery(pageNumber, pageSize, campaignId, voucherId, userId, dateFrom, dateTo));
        return Ok(result);
    }

    /// <summary>
    /// EN: Get redemptions by campaign.
    /// VI: Lấy redemptions theo chiến dịch.
    /// </summary>
    [HttpGet("by-campaign/{campaignId:guid}")]
    [ProducesResponseType(typeof(PaginatedResponse<AdminRedemptionListDto>), StatusCodes.Status200OK)]
    public async Task<ActionResult<PaginatedResponse<AdminRedemptionListDto>>> GetRedemptionsByCampaign(
        Guid campaignId,
        [FromQuery] int pageNumber = 1,
        [FromQuery] int pageSize = 20)
    {
        var result = await _mediator.Send(new GetAllRedemptionsQuery(pageNumber, pageSize, campaignId, null, null, null, null));
        return Ok(result);
    }

    /// <summary>
    /// EN: Get redemptions by voucher.
    /// VI: Lấy redemptions theo voucher.
    /// </summary>
    [HttpGet("by-voucher/{voucherId:guid}")]
    [ProducesResponseType(typeof(PaginatedResponse<AdminRedemptionListDto>), StatusCodes.Status200OK)]
    public async Task<ActionResult<PaginatedResponse<AdminRedemptionListDto>>> GetRedemptionsByVoucher(
        Guid voucherId,
        [FromQuery] int pageNumber = 1,
        [FromQuery] int pageSize = 20)
    {
        var result = await _mediator.Send(new GetAllRedemptionsQuery(pageNumber, pageSize, null, voucherId, null, null, null));
        return Ok(result);
    }

    /// <summary>
    /// EN: Get redemption statistics.
    /// VI: Lấy thống kê redemption.
    /// </summary>
    [HttpGet("statistics")]
    [ProducesResponseType(typeof(AdminRedemptionStatisticsDto), StatusCodes.Status200OK)]
    public async Task<ActionResult<AdminRedemptionStatisticsDto>> GetRedemptionStatistics(
        [FromQuery] Guid? campaignId = null)
    {
        var result = await _mediator.Send(new GetRedemptionStatisticsQuery(campaignId));
        return Ok(result);
    }
}
