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 Campaign management.
/// VI: Admin controller để quản lý Campaign.
/// </summary>
[ApiController]
[Route("api/v1/admin/campaigns")]
[Authorize(Roles = "Admin")]
[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;
        _logger = logger;
    }

    /// <summary>
    /// EN: Get all campaigns with pagination and filters.
    /// VI: Lấy tất cả chiến dịch với phân trang và bộ lọc.
    /// </summary>
    [HttpGet]
    [ProducesResponseType(typeof(PaginatedResponse<AdminCampaignListDto>), StatusCodes.Status200OK)]
    public async Task<ActionResult<PaginatedResponse<AdminCampaignListDto>>> GetAllCampaigns(
        [FromQuery] int pageNumber = 1,
        [FromQuery] int pageSize = 20,
        [FromQuery] Guid? merchantId = null,
        [FromQuery] string? status = null,
        [FromQuery] string? searchTerm = null)
    {
        var result = await _mediator.Send(new GetAllCampaignsQuery(pageNumber, pageSize, merchantId, status, searchTerm));
        return Ok(result);
    }

    /// <summary>
    /// EN: Get campaign statistics.
    /// VI: Lấy thống kê chiến dịch.
    /// </summary>
    [HttpGet("{id:guid}/statistics")]
    [ProducesResponseType(typeof(AdminCampaignStatisticsDto), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<ActionResult<AdminCampaignStatisticsDto>> GetCampaignStatistics(Guid id)
    {
        var result = await _mediator.Send(new GetAdminCampaignStatisticsQuery(id));
        return result == null ? NotFound() : Ok(result);
    }

    /// <summary>
    /// EN: Get vouchers for a campaign.
    /// VI: Lấy vouchers của chiến dịch.
    /// </summary>
    [HttpGet("{id:guid}/vouchers")]
    [ProducesResponseType(typeof(PaginatedResponse<AdminVoucherListDto>), StatusCodes.Status200OK)]
    public async Task<ActionResult<PaginatedResponse<AdminVoucherListDto>>> GetCampaignVouchers(
        Guid id,
        [FromQuery] int pageNumber = 1,
        [FromQuery] int pageSize = 20,
        [FromQuery] string? status = null)
    {
        var result = await _mediator.Send(new GetCampaignVouchersQuery(id, pageNumber, pageSize, status));
        return Ok(result);
    }

    /// <summary>
    /// EN: Update campaign details.
    /// VI: Cập nhật thông tin chiến dịch.
    /// </summary>
    [HttpPut("{id:guid}")]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> UpdateCampaign(Guid id, [FromBody] UpdateCampaignRequest request)
    {
        var result = await _mediator.Send(new UpdateCampaignCommand(
            id, request.Name, request.Description, request.StartDate, request.EndDate, request.MaxPerUser));
        return result ? Ok() : NotFound();
    }

    /// <summary>
    /// EN: Force complete a campaign.
    /// VI: Bắt buộc hoàn thành chiến dịch.
    /// </summary>
    [HttpPost("{id:guid}/complete")]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> CompleteCampaign(Guid id)
    {
        var result = await _mediator.Send(new CompleteCampaignCommand(id));
        return result ? Ok() : NotFound();
    }

    /// <summary>
    /// EN: Delete (soft delete) a campaign.
    /// VI: Xóa (xóa mềm) chiến dịch.
    /// </summary>
    [HttpDelete("{id:guid}")]
    [ProducesResponseType(StatusCodes.Status204NoContent)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> DeleteCampaign(Guid id)
    {
        var result = await _mediator.Send(new DeleteCampaignCommand(id));
        return result ? NoContent() : NotFound();
    }
}

/// <summary>
/// EN: Request to update campaign.
/// VI: Request để cập nhật chiến dịch.
/// </summary>
public record UpdateCampaignRequest(
    string? Name = null,
    string? Description = null,
    DateTime? StartDate = null,
    DateTime? EndDate = null,
    int? MaxPerUser = null);
