using Asp.Versioning;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using MktXService.API.Application.Queries;

namespace MktXService.API.Controllers;

/// <summary>
/// EN: Controller for Template management.
/// VI: Controller quản lý template.
/// </summary>
[ApiController]
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[controller]")]
[Produces("application/json")]
public class TemplatesController : ControllerBase
{
    private readonly IMediator _mediator;
    private readonly ILogger<TemplatesController> _logger;

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

    /// <summary>
    /// EN: Get all templates for a merchant.
    /// VI: Lấy tất cả template của merchant.
    /// </summary>
    [HttpGet]
    [ProducesResponseType(typeof(IEnumerable<TemplateDto>), StatusCodes.Status200OK)]
    public async Task<IActionResult> GetTemplates(
        [FromQuery] Guid merchantId,
        [FromQuery] string? type = null)
    {
        var templates = await _mediator.Send(new GetTemplatesQuery(merchantId, type));
        return Ok(new { success = true, data = templates });
    }

    /// <summary>
    /// EN: Get a template by ID.
    /// VI: Lấy template theo ID.
    /// </summary>
    [HttpGet("{id:guid}")]
    [ProducesResponseType(typeof(TemplateDetailDto), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> GetTemplate(Guid id)
    {
        var template = await _mediator.Send(new GetTemplateQuery(id));
        if (template == null)
            return NotFound(new { success = false, error = new { code = "TEMPLATE_NOT_FOUND", message = $"Template {id} not found" } });

        return Ok(new { success = true, data = template });
    }
}
