<?php

namespace App\Jobs;

use App\Crawlers\ContentMigrationCrawlObserver;
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\Cache;
use Mtc\MercuryDataModels\ContentMigrationEntry;
use Spatie\Crawler\Crawler;
use Spatie\Crawler\CrawlProfiles\CrawlInternalUrls;

class CrawlSiteJob implements ShouldQueue
{
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;
    use SerializesModels;

    public $timeout = 600;

    public function __construct(
        private string $url,
        private string $cacheKey,
        private bool $clearExisting = false,
    ) {
        $this->onQueue('sync');
    }

    public function handle(): void
    {
        if ($this->clearExisting) {
            ContentMigrationEntry::query()->delete();
        }

        Cache::put($this->cacheKey, [
            'status' => 'crawling',
            'crawled' => 0,
            'imported' => 0,
        ], now()->addMinutes(30));

        $observer = new ContentMigrationCrawlObserver($this->cacheKey);

        Crawler::create()
            ->setCrawlObserver($observer)
            ->setCrawlProfile(new CrawlInternalUrls($this->url))
            ->setTotalCrawlLimit(5000)
            ->setDelayBetweenRequests(100)
            ->startCrawling($this->url);
    }

    public function failed(\Throwable $exception): void
    {
        Cache::put($this->cacheKey, [
            'status' => 'failed',
            'error' => $exception->getMessage(),
        ], now()->addMinutes(30));
    }
}
