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 Campaign management.
/// VI: Controller để quản lý Campaign.
/// </summary>
[ApiController]
[Route("api/v1/[controller]")]
[Produces("application/json")]
public class CampaignsController : ControllerBase
{
    private readonly IMediator _mediator;
    private readonly ILogger<CampaignsController> _logger;

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

    /// <summary>
    /// EN: Create a new campaign.
    /// VI: Tạo chiến dịch mới.
    /// </summary>
    [HttpPost]
    [Authorize(Roles = "Merchant,Admin")]
    [ProducesResponseType(typeof(CampaignDto), StatusCodes.Status201Created)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    public async Task<ActionResult<CampaignDto>> CreateCampaign([FromBody] CreateCampaignCommand command)
    {
        _logger.LogInformation("Creating campaign {Name}", command.Name);
        var result = await _mediator.Send(command);
        return CreatedAtAction(nameof(GetCampaign), new { id = result.Id }, result);
    }

    /// <summary>
    /// EN: Get campaign by ID.
    /// VI: Lấy chiến dịch theo ID.
    /// </summary>
    [HttpGet("{id:guid}")]
    [ProducesResponseType(typeof(CampaignDto), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<ActionResult<CampaignDto>> GetCampaign(Guid id)
    {
        var result = await _mediator.Send(new GetCampaignQuery(id));
        return result == null ? NotFound() : Ok(result);
    }

    /// <summary>
    /// EN: Get campaigns list.
    /// VI: Lấy danh sách chiến dịch.
    /// </summary>
    [HttpGet]
    [ProducesResponseType(typeof(IEnumerable<CampaignSummaryDto>), StatusCodes.Status200OK)]
    public async Task<ActionResult<IEnumerable<CampaignSummaryDto>>> GetCampaigns(
        [FromQuery] Guid? merchantId = null,
        [FromQuery] bool activeOnly = false)
    {
        var result = await _mediator.Send(new GetCampaignsQuery(merchantId, activeOnly));
        return Ok(result);
    }

    /// <summary>
    /// EN: Activate a campaign.
    /// VI: Kích hoạt chiến dịch.
    /// </summary>
    [HttpPost("{id:guid}/activate")]
    [Authorize(Roles = "Merchant,Admin")]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> ActivateCampaign(Guid id)
    {
        var result = await _mediator.Send(new ActivateCampaignCommand(id));
        return result ? Ok() : NotFound();
    }

    /// <summary>
    /// EN: Pause a campaign.
    /// VI: Tạm dừng chiến dịch.
    /// </summary>
    [HttpPost("{id:guid}/pause")]
    [Authorize(Roles = "Merchant,Admin")]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> PauseCampaign(Guid id)
    {
        var result = await _mediator.Send(new PauseCampaignCommand(id));
        return result ? Ok() : NotFound();
    }

    /// <summary>
    /// EN: Cancel a campaign.
    /// VI: Hủy chiến dịch.
    /// </summary>
    [HttpPost("{id:guid}/cancel")]
    [Authorize(Roles = "Merchant,Admin")]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> CancelCampaign(Guid id)
    {
        var result = await _mediator.Send(new CancelCampaignCommand(id));
        return result ? Ok() : NotFound();
    }
}
