<?php

namespace App\Jobs;

use App\Models\RssPost;
use App\Models\RssPostKeyword;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

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

    protected $rss_post_id;

    public $timeout = 15;

    /**
     * Create a new job instance.
     */
    public function __construct($rss_post_id)
    {
        $this->rss_post_id = $rss_post_id;
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        $rss_post = RssPost::find($this->rss_post_id);

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

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

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

        $first_keyword_found = false;

        // Entities
        if (isset($rss_post->entities)) {
            if (count($rss_post->entities) > 0) {

                foreach ($rss_post->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;
                }
            }
        }

        // Keywords
        if (isset($rss_post->keywords)) {
            if (count($rss_post->keywords) > 0) {

                foreach ($rss_post->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;
                    }

                }
            }
        }

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

        $rss_post->status = 'published';

        if ($rss_post->save()) {
            $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();
            }
        }

    }
}
