#nullable enable
using Baya.Application.Contracts.Common;
using Baya.Application.Contracts.Persistence;
using Baya.Application.Models.Common;
using Baya.Application.Models.Refunds;
using Mediator;

namespace Baya.Application.Features.Refunds.Queries.GetRefundStatus;

internal sealed class GetRefundStatusQueryHandler(
    IUnitOfWork unitOfWork,
    ICurrentUser currentUser)
    : IRequestHandler<GetRefundStatusQuery, OperationResult<RefundStatusDto>>
{
    public async ValueTask<OperationResult<RefundStatusDto>> Handle(GetRefundStatusQuery request, CancellationToken cancellationToken)
    {
        var projection = await unitOfWork.RefundRepository.GetStatusAsync(request.RefundId, cancellationToken);

        // A cross-customer access is indistinguishable from "not found" — never confirm the refund exists.
        if (projection is null || projection.CustomerUserId != currentUser.UserId)
            return OperationResult<RefundStatusDto>.NotFoundResult("Refund not found.");

        return OperationResult<RefundStatusDto>.SuccessResult(projection.Refund);
    }
}
