#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.GetRefundByBooking;

internal sealed class GetRefundByBookingQueryHandler(
    IUnitOfWork unitOfWork,
    ICurrentUser currentUser)
    : IRequestHandler<GetRefundByBookingQuery, OperationResult<RefundStatusDto>>
{
    public async ValueTask<OperationResult<RefundStatusDto>> Handle(GetRefundByBookingQuery request, CancellationToken cancellationToken)
    {
        var projection = await unitOfWork.RefundRepository.GetStatusByBookingAsync(request.BookingId, cancellationToken);

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

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