<?php

namespace App\Jobs;

use App\Repositories\BillingRepository;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class GenerateInvoiceJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected int $billingId;

    public function __construct(int $billingId)
    {
        $this->billingId = $billingId;
    }

    public function handle(BillingRepository $repo): void
    {
        $billing = $repo->find($this->billingId);
        if (! $billing) {
            return;
        }

        $billing->forceFill(['invoice_id' => $billing->invoice_id ?: $billing->id])->save();
    }
}
