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

namespace PromotionService.API.Controllers;

/// <summary>
/// EN: Controller for Voucher operations.
/// VI: Controller cho các thao tác Voucher.
/// </summary>
[ApiController]
[Route("api/v1/[controller]")]
[Produces("application/json")]
public class VouchersController : ControllerBase
{
    private readonly IMediator _mediator;
    private readonly ILogger<VouchersController> _logger;

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

    /// <summary>
    /// EN: Claim a free voucher.
    /// VI: Nhận voucher miễn phí.
    /// </summary>
    [HttpPost("claim")]
    [Authorize]
    [ProducesResponseType(typeof(VoucherDto), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    public async Task<ActionResult<VoucherDto>> ClaimVoucher([FromBody] ClaimVoucherCommand command)
    {
        var result = await _mediator.Send(command);
        return Ok(result);
    }

    /// <summary>
    /// EN: Validate a voucher code.
    /// VI: Xác thực mã voucher.
    /// </summary>
    [HttpGet("validate/{code}")]
    [Authorize]
    [ProducesResponseType(typeof(VoucherValidationDto), StatusCodes.Status200OK)]
    public async Task<ActionResult<VoucherValidationDto>> ValidateVoucher(string code, [FromQuery] Guid userId)
    {
        var result = await _mediator.Send(new ValidateVoucherQuery(code, userId));
        return Ok(result);
    }

    /// <summary>
    /// EN: Redeem a voucher.
    /// VI: Sử dụng voucher.
    /// </summary>
    [HttpPost("redeem")]
    [Authorize]
    [ProducesResponseType(typeof(RedemptionDto), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    public async Task<ActionResult<RedemptionDto>> RedeemVoucher([FromBody] RedeemVoucherCommand command)
    {
        var result = await _mediator.Send(command);
        return Ok(result);
    }

    /// <summary>
    /// EN: Get user's vouchers.
    /// VI: Lấy voucher của người dùng.
    /// </summary>
    [HttpGet("user/{userId:guid}")]
    [Authorize]
    [ProducesResponseType(typeof(IEnumerable<VoucherSummaryDto>), StatusCodes.Status200OK)]
    public async Task<ActionResult<IEnumerable<VoucherSummaryDto>>> GetUserVouchers(Guid userId)
    {
        var result = await _mediator.Send(new GetUserVouchersQuery(userId));
        return Ok(result);
    }
}
