<?php

namespace App\Jobs\Tasks;

use App\Helpers\FirstParty\DFS\DFSSerp;
use App\Helpers\FirstParty\OpenAI\OpenAI;
use App\Helpers\ThirdParty\DFS\SettingSerpLiveAdvanced;
use App\Jobs\CrawlUrlResearchJob;
use App\Models\SerpUrl;
use App\Models\SerpUrlResearch;
use App\Models\ServiceCostUsage;
use Exception;

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

        if ((! is_null($serp_url)) && (! is_null($serp_url->suggestion_data))) {
            if (isset($serp_url->suggestion_data->proposed_search_queries)) {
                if (count($serp_url->suggestion_data->proposed_search_queries) > 0) {
                    $search_query = $serp_url->suggestion_data->proposed_search_queries[0];

                    // $serp_model = new SettingSerpLiveAdvanced;

                    // $serp_model->setSe('google');
                    // $serp_model->setSeType('organic');
                    // $serp_model->setKeyword(strtolower($search_query));
                    // $serp_model->setLocationName('United States');
                    // //$serp_model->setDepth(100);
                    // $serp_model->setLanguageCode('en');
                    // $serp_res = $serp_model->getAsJson();

                    // print_r($serp_res);
                    // die();
                    $country_name = get_country_name_by_iso($serp_url->country_iso);

                    $serp_res = DFSSerp::liveAdvanced('google', 'news', $search_query, $country_name, 'en', 100);

                    try {
                        $serp_obj = json_decode($serp_res, false, 512, JSON_THROW_ON_ERROR);

                        if ($serp_obj?->status_code == 20000) {

                            $service_cost_usage = new ServiceCostUsage;
                            $service_cost_usage->cost = $serp_obj->cost;
                            $service_cost_usage->name = 'dataforseo-GoogleSerpApiAdvancedLiveOrganic';
                            $service_cost_usage->reference_1 = 'google';
                            $service_cost_usage->reference_2 = 'organic';
                            $service_cost_usage->output = $serp_obj;
                            $service_cost_usage->input_1 = $country_name;
                            $service_cost_usage->input_2 = $search_query;
                            $service_cost_usage->save();

                            $results = $serp_obj?->tasks[0]->result[0]?->items;

                            //$results = $serp_obj?->result[0]?->items;

                            // dump($serp_obj);
                            // exit();

                            $saved_count = 0;

                            $first_serp_url_research = null;

                            foreach ($results as $key => $result) {
                                if ($result->type == 'news_search') {
                                    $serp_url_research = SerpUrlResearch::where('url', $result->url)->where('serp_url_id', $serp_url_id)->first();

                                    if (is_null($serp_url_research)) {
                                        //dump($result->url);

                                        $serp_url_research = new SerpUrlResearch;
                                        $serp_url_research->serp_url_id = $serp_url_id;
                                        $serp_url_research->url = $result->url;
                                        $serp_url_research->query = $search_query;
                                        $serp_url_research->content = null;
                                        if ($serp_url_research->save()) {
                                            $saved_count++;
                                        }
                                    }
                                }
                                if ($saved_count >= 10) {
                                    break;
                                }
                            }

                            $first_serp_url_research = SerpUrlResearch::where('serp_url_id', $serp_url_id)->orderBy('created_at', 'ASC')->whereNull('content')->first();

                            CrawlUrlResearchJob::dispatch($first_serp_url_research->id)->onQueue('default')->onConnection('default');
                        }
                    } catch (Exception $e) {
                        throw $e;
                    }

                }
            }
        }
    }
}

// {
//    "identified_keywords":[
//       "Humane AI Pin",
//       "costs",
//       "OpenAI",
//       "T-Mobile integration"
//    ],
//    "related_keywords":[
//       "artificial intelligence device",
//       "monthly subscription",
//       "OpenAI partnership",
//       "T-Mobile collaboration"
//    ],
//    "proposed_search_queries":[
//       "Humane AI Pin features",
//       "Cost of Humane AI Pin",
//       "Humane AI Pin integration with OpenAI and T-Mobile",
//       "Reviews of Humane AI Pin"
//    ],
//    "writing_tone":[
//       "engaging",
//       "informative"
//    ],
//    "article_headings":[
//       "Introduction to Humane AI Pin",
//       "Features of Humane AI Pin",
//       "Cost and Subscription Details",
//       "OpenAI and T-Mobile Integration"
//    ]
// }
