<?php

namespace App\Jobs\Tasks;

use App\Helpers\FirstParty\OpenAI\OpenAI;
use App\Jobs\BrowseDFSForResearchJob;
use App\Models\SerpUrl;
use App\Models\ServiceCostUsage;

class IdentifyCrawlSourcesTask
{
    public static function handle(int $serp_url_id)
    {
        $serp_url = SerpUrl::find($serp_url_id);

        if (! is_null($serp_url)) {

            $attempt = 0;
            $maxAttempts = 3;
            $suggestion_response = null;

            while ($attempt < $maxAttempts && ($suggestion_response === null || $suggestion_response->output === null)) {
                $suggestion_response = OpenAI::titleSuggestions($serp_url->title);

                //dump($suggestion_response);

                $service_cost_usage = new ServiceCostUsage;
                $service_cost_usage->cost = $suggestion_response->cost;
                $service_cost_usage->name = 'openai-titleSuggestions';
                $service_cost_usage->reference_1 = 'serp_url';
                $service_cost_usage->reference_2 = strval($serp_url->id);
                $service_cost_usage->output = $suggestion_response;
                $service_cost_usage->save();

                $attempt++;

                // If the output is not null, break out of the loop
                if ($suggestion_response !== null && $suggestion_response->output !== null) {
                    break;
                }

                // Optional: sleep for a bit before retrying
                sleep(1); // sleep for 1 second
            }

            if (! is_null($suggestion_response->output)) {
                $serp_url->suggestion_data = $suggestion_response->output;
                if ($serp_url->save()) {
                    BrowseDFSForResearchJob::dispatch($serp_url_id)->onQueue('default')->onConnection('default');
                }
            }
        }
    }
}
