<?php

namespace App\Jobs\Tasks;

use App\Helpers\FirstParty\OpenAI\OpenAI;
use App\Models\Category;
use App\Models\RssPost;
use App\Models\RssPostKeyword;
use App\Models\ServiceCostUsage;

class ParseRssPostMetadataTask
{
    public static function handle(int $rss_post_id)
    {
        $rss_post = RssPost::find($rss_post_id);

        if (is_null($rss_post)) {
            return;
        }

        if (count_words($rss_post->body) < 250)
        {
          $rss_post->status = 'blocked';
          $rss_post->save();

          return;
        }

        if (in_array($rss_post->status, ['blocked', 'trashed'])) {
            return;
        }

        if (! is_null($rss_post->metadata)) {
            $post_meta_response = $rss_post->metadata;
        } else {
            $post_meta_response = OpenAI::getRssPostMetaGpt3($rss_post->body, 1536, 30);

            if ((isset($post_meta_response->output)) && (! is_null($post_meta_response->output))) {
                $service_cost_usage = new ServiceCostUsage;
                $service_cost_usage->cost = $post_meta_response->cost;
                $service_cost_usage->name = 'openai-getRssPostMetaGpt3';
                $service_cost_usage->reference_1 = 'rss_post';
                $service_cost_usage->reference_2 = strval($rss_post->id);
                $service_cost_usage->output = $post_meta_response;
                $service_cost_usage->save();
            }
        }

        $words_to_add_in_keyword_list = [];
        $words_to_save = [];

        $first_keyword_found = false;

        if ((isset($post_meta_response->output)) && (! is_null($post_meta_response->output))) {

            $rss_post->metadata = $post_meta_response;

            if (isset($post_meta_response->output->title)) {
                if (! is_empty($post_meta_response->output->title)) {
                    $rss_post->title = $post_meta_response->output->title;
                    $rss_post->slug = ($post_meta_response->output->title);
                }
            }

            if (isset($post_meta_response->output->entities)) {
                if (count($post_meta_response->output->entities) > 0) {
                    $rss_post->entities = $post_meta_response->output->entities;

                    foreach ($post_meta_response->output->entities as $key => $word) {

                        $word = trim($word);

                        $words_to_save[] = (object) [
                            'is_main' => ($key == 0) ? true : false,
                            'type' => 'entity',
                            'value' => $word,
                            'value_lowercased' => strtolower($word),
                        ];

                        $words_to_add_in_keyword_list[] = $word;
                    }
                }
            }

            if (isset($post_meta_response->output->keywords)) {
                if (count($post_meta_response->output->keywords) > 0) {
                    $rss_post->keywords = $post_meta_response->output->keywords;

                    foreach ($post_meta_response->output->keywords as $word) {

                        $word = trim($word);

                        foreach ($words_to_save as $saved_word) {
                            if (strtolower($word) == $saved_word->value_lowercased) {
                                continue 2;
                            }
                        }

                        $words_to_save[] = (object) [
                            'is_main' => ! $first_keyword_found,
                            'type' => 'keyword',
                            'value' => $word,
                            'value_lowercased' => strtolower($word),
                        ];

                        $words_to_add_in_keyword_list[] = $word;

                        if ($first_keyword_found == false) {
                            $first_keyword_found = true;
                        }

                    }
                }
            }

            if (isset($post_meta_response->output->summary)) {
                if (! is_empty($post_meta_response->output->summary)) {
                    $rss_post->bites = $post_meta_response->output->summary;
                }
            }

            if (isset($post_meta_response->output->society_impact)) {
                if (! is_empty($post_meta_response->output->society_impact)) {
                    $rss_post->impact = $post_meta_response->output->society_impact;
                }
            }

            if (isset($post_meta_response->output->society_impact_level)) {
                if (! is_empty($post_meta_response->output->society_impact_level)) {
                    $rss_post->impact_level = $post_meta_response->output->society_impact_level;
                }
            }

            // Category
            $category_name = 'Updates';

            if ((isset($post_meta_response->output->category)) && (! is_empty($post_meta_response->output->category))) {
                $category_name = $post_meta_response?->output?->category;
            }

            $category = Category::where('name', $category_name)->first();

            if (is_null($category)) {
                $category = Category::where('name', 'Updates')->first();
            }

            $rss_post->category_id = $category->id;
        }

        $rss_post->keyword_list = implode(',', $words_to_add_in_keyword_list);

        if (is_empty($rss_post->bites)) {
            $rss_post->status = 'blocked';
        } else {
            $rss_post->status = 'published';
        }

        if (!$rss_post->status != 'blocked')
        {
            if (isset($post_meta_response->output->is_ai_or_tech_news))
            {
                if ($post_meta_response->output->is_ai_or_tech_news != true)
                {
                    $rss_post->status = 'blocked';
                }
                // else
                // {
                //   if (isset($post_meta_response->output->is_government_or_political_news))
                //   {
                //     if($post_meta_response->output->is_government_or_political_news == true)
                //     {
                //       $rss_post->status = 'blocked';
                //     }
                //   }
                // }
            }

        }

        if ($rss_post->save()) {

            if ($rss_post->status == 'published') {

                $has_saved_keyword = false;

                $deleted_rpk = RssPostKeyword::where('rss_post_id', $rss_post->id)->delete();

                foreach ($words_to_save as $word_to_save) {

                    $new_rpk = new RssPostKeyword;
                    $new_rpk->rss_post_id = $rss_post->id;
                    $new_rpk->type = $word_to_save->type;
                    $new_rpk->is_main = $word_to_save->is_main;
                    $new_rpk->value = $word_to_save->value;
                    $new_rpk->value_lowercased = $word_to_save->value_lowercased;
                    $new_rpk->created_at = $rss_post->published_at;
                    $new_rpk->updated_at = $rss_post->published_at;

                    if ($new_rpk->save()) {
                        if (! $has_saved_keyword) {
                            $has_saved_keyword = true;
                        }

                    }
                }

                if ($has_saved_keyword) {
                    $rss_post->keyword_saved = true;
                    $rss_post->save();
                }

            }

        }

    }
}
