<?php

namespace App\Jobs;

use App\Jobs\Tasks\BrowseDFSLatestNewsTask;
use App\Jobs\Tasks\ParseDFSNewsTask;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

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

    public $timeout = 20;

    /**
     * Create a new job instance.
     */
    public function __construct()
    {
        //
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        $news_serp_result = BrowseDFSLatestNewsTask::handle('ai', 'US');

        if (! is_null($news_serp_result)) {
            ParseDFSNewsTask::handle($news_serp_result);
        } else {
            throw new Exception('Empty news_serp_result. API error?');
        }

    }
}
