<?php

declare(strict_types=1);

namespace App\Jobs\Paynl;

use App\Models\Merchant;
use App\Services\PaynlService;
use Arr;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

final class FetchTerminalsForMerchant implements ShouldQueue
{
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;
    use SerializesModels;

    public function __construct(
        protected readonly Merchant $merchant,
    ) {
    }

    public function handle(PaynlService $paynlService): void
    {
        $data = $paynlService->terminals($this->merchant->pay_id);

        if (empty($data)) {
            return;
        }

        $this->merchant->update([
            'amount_of_terminals' => Arr::get($data, 'total') ?? 0,
        ]);
    }
}
