<?php

namespace App\Jobs\Site;

use App\Models\Agency\Agency;
use App\Services\Site\DomainProvisioningService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

/** Doğrulanmış domaini sunucuya alias olarak ekler ve AutoSSL tetikler. İdempotent. */
class ProvisionDomainJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public int $tries = 3;
    public array $backoff = [60, 300];

    public function __construct(public int $agencyId)
    {
    }

    public function handle(DomainProvisioningService $service): void
    {
        $agency = Agency::find($this->agencyId);
        if (! $agency || ! $agency->custom_domain_verified) {
            return;
        }

        $service->provision($agency);
    }
}
