using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.RateLimiting;
using System.ComponentModel.DataAnnotations;
using TakeoutSaaS.Application.App.Tenants.Dto;
using TakeoutSaaS.Application.App.Tenants.Queries;
using TakeoutSaaS.Shared.Abstractions.Results;
using TakeoutSaaS.Shared.Web.Api;

namespace TakeoutSaaS.AdminApi.Controllers;

/// <summary>
/// 公共租户套餐查询接口。
/// </summary>
[ApiVersion("1.0")]
[AllowAnonymous]
[EnableRateLimiting("public-self-service")]
[Route("api/public/v{version:apiVersion}/tenant-packages")]
public sealed class PublicTenantPackagesController(IMediator mediator) : BaseApiController
{
    /// <summary>
    /// 分页获取已启用的租户套餐。
    /// </summary>
    /// <param name="query">分页参数。</param>
    /// <param name="cancellationToken">取消标记。</param>
    /// <returns>启用套餐的分页列表。</returns>
    [HttpGet]
    [ProducesResponseType(typeof(ApiResponse<PagedResult<TenantPackageDto>>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<PagedResult<TenantPackageDto>>> List(
        [FromQuery, Required] GetPublicTenantPackagesQuery query,
        CancellationToken cancellationToken)
    {
        // 1. 执行查询
        var result = await mediator.Send(query, cancellationToken);
        // 2. 返回结果
        return ApiResponse<PagedResult<TenantPackageDto>>.Ok(result);
    }
}
