<?php

namespace App\Jobs\Tasks;

use App\Models\Post;
use Exception;
use LaravelFreelancerNL\LaravelIndexNow\Facades\IndexNow;
use LaravelGoogleIndexing;

class PublishIndexPostTask
{
    public static function handle(int $post_id)
    {
        $post = Post::find($post_id);

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

        $post->status = 'publish';

        if ($post->save()) {
            if ((app()->environment() == 'production') && (config('platform.global.indexing'))) {
                $post_url = route('front.post', ['slug' => $post->slug, 'category_slug' => $post->category->slug]);

                try {
                    IndexNow::submit($post_url);
                } catch (Exception) {
                }

                try {
                    LaravelGoogleIndexing::create()->update($post_url);
                } catch (Exception) {
                }

            }

        }
    }
}
