using System.Collections.Generic;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using TakeoutSaaS.Application.App.Stores.Dto;
using TakeoutSaaS.Application.App.Stores.Queries;
using TakeoutSaaS.Shared.Abstractions.Constants;
using TakeoutSaaS.Shared.Abstractions.Results;
using TakeoutSaaS.Shared.Web.Api;

namespace TakeoutSaaS.MiniApi.Controllers;

/// <summary>
/// 桌码上下文。
/// </summary>
[ApiVersion("1.0")]
[Authorize]
[Route("api/mini/v{version:apiVersion}/tables")]
public sealed class TablesController(IMediator mediator) : BaseApiController
{
    /// <summary>
    /// 解析桌码并返回上下文。
    /// </summary>
    [HttpGet("{code}/context")]
    [ProducesResponseType(typeof(ApiResponse<StoreTableContextDto>), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status404NotFound)]
    public async Task<ApiResponse<StoreTableContextDto>> GetContext(string code, CancellationToken cancellationToken)
    {
        var result = await mediator.Send(new GetStoreTableContextQuery { TableCode = code }, cancellationToken);
        return result is null
            ? ApiResponse<StoreTableContextDto>.Error(ErrorCodes.NotFound, "桌码不存在")
            : ApiResponse<StoreTableContextDto>.Ok(result);
    }
}
