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.Admin;

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

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

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

    /// <summary>
    /// EN: Search vouchers by code.
    /// VI: Tìm kiếm vouchers theo mã.
    /// </summary>
    [HttpGet("search")]
    [ProducesResponseType(typeof(IEnumerable<AdminVoucherListDto>), StatusCodes.Status200OK)]
    public async Task<ActionResult<IEnumerable<AdminVoucherListDto>>> SearchVouchers(
        [FromQuery] string q)
    {
        var result = await _mediator.Send(new SearchVouchersQuery(q));
        return Ok(result);
    }

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

    /// <summary>
    /// EN: Revoke a voucher.
    /// VI: Thu hồi voucher.
    /// </summary>
    [HttpPost("{id:guid}/revoke")]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> RevokeVoucher(Guid id, [FromBody] RevokeVoucherRequest request)
    {
        var result = await _mediator.Send(new RevokeVoucherCommand(id, request.Reason));
        return result ? Ok() : NotFound();
    }

    /// <summary>
    /// EN: Extend voucher expiry.
    /// VI: Gia hạn voucher.
    /// </summary>
    [HttpPost("{id:guid}/extend")]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> ExtendVoucherExpiry(Guid id, [FromBody] ExtendExpiryRequest request)
    {
        var result = await _mediator.Send(new ExtendVoucherExpiryCommand(id, request.AdditionalDays));
        return result ? Ok() : NotFound();
    }
}

public record RevokeVoucherRequest(string Reason);
public record ExtendExpiryRequest(int AdditionalDays);
