<?php

namespace App\Jobs\StripeWebhooks;

use App\Http\Helpers\StripeClient;
use App\Models\Transaction;
use App\Models\Wallet;
use App\Notifications\PayoutDelivered;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Spatie\WebhookClient\Models\WebhookCall;
use Stripe\Event;

class HandlePayoutSuccess implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    /** @var \Spatie\WebhookClient\Models\WebhookCall */
    public $webhookCall;

    public function __construct(WebhookCall $webhookCall)
    {
        $this->webhookCall = $webhookCall;
    }

    public function handle()
    {
        $webhookCall = $this->webhookCall;
        $payout = Event::constructFrom($webhookCall->payload)->data?->object;
        Transaction::where('stripe_id',$payout->id)->first()->toWallet->owner->notify(new PayoutDelivered());
    }
}
