using MediatR;
using TakeoutSaaS.Application.App.Members.PointsMall.Dto;
using TakeoutSaaS.Application.App.Members.PointsMall.Queries;
using TakeoutSaaS.Domain.Membership.Repositories;
using TakeoutSaaS.Shared.Abstractions.Constants;
using TakeoutSaaS.Shared.Abstractions.Exceptions;
using TakeoutSaaS.Shared.Abstractions.Tenancy;

namespace TakeoutSaaS.Application.App.Members.PointsMall.Handlers;

/// <summary>
/// 查询积分商城兑换记录详情处理器。
/// </summary>
public sealed class GetPointMallRecordDetailQueryHandler(
    IPointMallRepository repository,
    ITenantProvider tenantProvider)
    : IRequestHandler<GetPointMallRecordDetailQuery, MemberPointMallRecordDetailDto>
{
    /// <inheritdoc />
    public async Task<MemberPointMallRecordDetailDto> Handle(
        GetPointMallRecordDetailQuery request,
        CancellationToken cancellationToken)
    {
        var tenantId = tenantProvider.GetCurrentTenantId();

        var record = await repository.GetRecordByIdAsync(
            tenantId,
            request.StoreId,
            request.RecordId,
            cancellationToken)
            ?? throw new BusinessException(ErrorCodes.NotFound, "兑换记录不存在");

        return MemberPointMallDtoFactory.ToRecordDetailDto(record);
    }
}
