<?php

namespace App\Classes\Modules\Transactions\Services;

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

class CalculatesBillGroupPaymentAmount
{
    public function execute(BillGroup $billGroup)
    {
        $bill_refund_amount = round(floatval($billGroup->billRefunds->sum('amount')), 7);
        $floating_amount = round(floatval($billGroup->transactions()->whereIn('status', [ApprovalStatus::PENDING_SUBMISSION, ApprovalStatus::PENDING_VERIFICATION])->sum('amount')), 7);
        $paid_amount = round(floatval($billGroup->transactions()->where('status', ApprovalStatus::APPROVED)->sum('amount')), 7);
        $outstanding_amount = $billGroup->amount - $paid_amount - $floating_amount;
        $outstanding_amount = round($outstanding_amount, 7);

        return [
            'bill_refund_amount' => $bill_refund_amount,
            'floating_amount' => $floating_amount,
            'paid_amount' => $paid_amount,
            'outstanding_amount' => $outstanding_amount,
        ];
    }
}
