<?php

namespace App\Jobs;

use App\Services\Files\CdnService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

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

    /**
     * Create a new job instance.
     */
    public function __construct(
        protected array $paths
    ) {
        $this->onQueue('cdn');
    }

    /**
     * Execute the job.
     */
    public function handle(CdnService $cdnService): void
    {
        if (! $cdnService->isEnabled()) {
            return;
        }

        $cdnService->invalidateCache($this->paths);

        Log::info('CDN cache invalidated', [
            'paths' => $this->paths,
        ]);
    }
}
