// EN: Controller for kitchen display system.
// VI: Controller cho hệ thống hiển thị bếp.

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

namespace FnbEngine.API.Controllers;

[ApiController]
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/kitchen")]
public class KitchenController : ControllerBase
{
    private readonly IMediator _mediator;

    public KitchenController(IMediator mediator)
    {
        _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
    }

    /// <summary>
    /// EN: Get pending kitchen tickets.
    /// VI: Lấy danh sách phiếu bếp chờ.
    /// </summary>
    [HttpGet("tickets")]
    [ProducesResponseType(typeof(ApiResponse<IEnumerable<KitchenTicketDto>>), 200)]
    public async Task<ActionResult<ApiResponse<IEnumerable<KitchenTicketDto>>>> GetPendingTickets(
        [FromQuery] string? station = null,
        CancellationToken ct = default)
    {
        var result = await _mediator.Send(new GetPendingTicketsQuery(station), ct);
        return Ok(new ApiResponse<IEnumerable<KitchenTicketDto>> { Success = true, Data = result });
    }

    /// <summary>
    /// EN: Update ticket status.
    /// VI: Cập nhật trạng thái phiếu.
    /// </summary>
    [HttpPatch("tickets/{id}/status")]
    [ProducesResponseType(typeof(ApiResponse<bool>), 200)]
    [ProducesResponseType(404)]
    public async Task<ActionResult<ApiResponse<bool>>> UpdateStatus(
        Guid id,
        [FromBody] UpdateStatusRequest request,
        CancellationToken ct = default)
    {
        var result = await _mediator.Send(new UpdateTicketStatusCommand(id, request.Status), ct);
        return Ok(new ApiResponse<bool> { Success = true, Data = result });
    }
}

public record UpdateStatusRequest(string Status);
