// EN: Controller for table management.
// VI: Controller quản lý bàn.

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

namespace FnbEngine.API.Controllers;

/// <summary>
/// EN: Controller for table management.
/// VI: Controller quản lý bàn.
/// </summary>
[ApiController]
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/tables")]
public class TablesController : ControllerBase
{
    private readonly IMediator _mediator;
    private readonly ILogger<TablesController> _logger;

    public TablesController(IMediator mediator, ILogger<TablesController> logger)
    {
        _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));
    }

    /// <summary>
    /// EN: Get tables by shop ID.
    /// VI: Lấy danh sách bàn theo shop ID.
    /// </summary>
    [HttpGet]
    [ProducesResponseType(typeof(ApiResponse<IEnumerable<TableDto>>), 200)]
    public async Task<ActionResult<ApiResponse<IEnumerable<TableDto>>>> GetTables(
        [FromQuery] Guid shopId,
        CancellationToken ct = default)
    {
        var result = await _mediator.Send(new GetTablesQuery(shopId), ct);
        return Ok(new ApiResponse<IEnumerable<TableDto>> { Success = true, Data = result });
    }

    /// <summary>
    /// EN: Create a new table.
    /// VI: Tạo bàn mới.
    /// </summary>
    [HttpPost]
    [ProducesResponseType(typeof(ApiResponse<CreateTableResult>), 201)]
    [ProducesResponseType(400)]
    public async Task<ActionResult<ApiResponse<CreateTableResult>>> CreateTable(
        [FromBody] CreateTableRequest request,
        CancellationToken ct = default)
    {
        var command = new CreateTableCommand(
            request.ShopId,
            request.TableNumber,
            request.Capacity,
            request.Zone);

        var result = await _mediator.Send(command, ct);
        return CreatedAtAction(nameof(GetTables), new { shopId = request.ShopId },
            new ApiResponse<CreateTableResult> { Success = true, Data = result });
    }

    /// <summary>
    /// EN: Change table status.
    /// VI: Đổi trạng thái bàn.
    /// </summary>
    [HttpPatch("{id}/status")]
    [ProducesResponseType(typeof(ApiResponse<bool>), 200)]
    [ProducesResponseType(404)]
    public async Task<ActionResult<ApiResponse<bool>>> ChangeStatus(
        Guid id,
        [FromBody] ChangeStatusRequest request,
        CancellationToken ct = default)
    {
        var command = new ChangeTableStatusCommand(id, request.Status);
        var result = await _mediator.Send(command, ct);
        return Ok(new ApiResponse<bool> { Success = true, Data = result });
    }
}

/// <summary>
/// EN: Request to create a table.
/// VI: Request tạo bàn.
/// </summary>
public record CreateTableRequest(
    Guid ShopId,
    string TableNumber,
    int Capacity,
    string? Zone = null);

/// <summary>
/// EN: Request to change table status.
/// VI: Request đổi trạng thái bàn.
/// </summary>
public record ChangeStatusRequest(string Status);

/// <summary>
/// EN: Standard API response wrapper.
/// VI: Wrapper response API chuẩn.
/// </summary>
public class ApiResponse<T>
{
    public bool Success { get; set; }
    public T? Data { get; set; }
    public string? Error { get; set; }
}
