using MediatR;
using TakeoutSaaS.Application.App.QuotaPackages.Dto;
using TakeoutSaaS.Application.App.QuotaPackages.Queries;
using TakeoutSaaS.Domain.Tenants.Repositories;
using TakeoutSaaS.Shared.Abstractions.Results;

namespace TakeoutSaaS.Application.App.QuotaPackages.Handlers;

/// <summary>
/// 获取配额包列表查询处理器。
/// </summary>
public sealed class GetQuotaPackageListQueryHandler(IQuotaPackageRepository quotaPackageRepository)
    : IRequestHandler<GetQuotaPackageListQuery, PagedResult<QuotaPackageListDto>>
{
    /// <inheritdoc />
    public async Task<PagedResult<QuotaPackageListDto>> Handle(GetQuotaPackageListQuery request, CancellationToken cancellationToken)
    {
        // 1. 分页查询
        var (items, total) = await quotaPackageRepository.SearchPagedAsync(
            request.QuotaType,
            request.IsActive,
            request.Page,
            request.PageSize,
            cancellationToken);

        // 2. 映射为 DTO
        var dtos = items.Select(x => new QuotaPackageListDto
        {
            Id = x.Id,
            Name = x.Name,
            QuotaType = x.QuotaType,
            QuotaValue = x.QuotaValue,
            Price = x.Price,
            IsActive = x.IsActive,
            SortOrder = x.SortOrder
        }).ToList();

        // 3. 返回分页结果
        return new PagedResult<QuotaPackageListDto>(dtos, request.Page, request.PageSize, total);
    }
}
