<?php

namespace App\Classes\Modules\Bookings\Services;

use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Models\Booking;

class CalculatesBookingRefundServiceCharge
{
    public function execute(Booking $booking, int $type, ?string $payment_reference = null): float
    {
        $refundAmounts = $booking->transactions()->payments()->complete()->get()->map(function ($payment) use ($type) {
            return $this->calculateRefundAmount($payment, $type);
        });

        $totalRefundAmount = $refundAmounts->sum();

        return $totalRefundAmount;
    }

    public function calculateRefundAmount($payment): float
    {
        $refundTransactions = $payment->transactions()->refunds()->whereIn('status', [ApprovalStatus::APPROVED]);

        return $refundTransactions->sum('service_charge');
    }
}
