<?php

namespace App\Http\Controllers\Tests;

use App\Helpers\FirstParty\ImageGen\ImageGen;
use App\Http\Controllers\Controller;
use App\Jobs\Tasks\BrowseRSSLatestNewsTask;
use App\Jobs\Tasks\CrawlRssPostTask;
use App\Jobs\Tasks\ParseRssPostMetadataTask;
use App\Models\Post;
use App\Notifications\PostWasPublished;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Notification;
use LaravelFreelancerNL\LaravelIndexNow\Facades\IndexNow;
use LaravelGoogleIndexing;

class TestController extends Controller
{
    public function blacklistkw(Request $request)
    {
      $country_names = get_country_names(true);

      dd($country_names);

    }

    public function prm(Request $request)
    {
        $id = $request->input('id');

        if (is_empty($id)) {
            return "Missing 'id'.";
        }

        ParseRssPostMetadataTask::handle($id);

        return 'ok';

    }

    public function crawlTask(Request $request)
    {
        $id = $request->input('id');

        if (is_empty($id)) {
            return "Missing 'id'.";
        }

        CrawlRssPostTask::handle($id);

        return 'ok';

    }

    public function opml(Request $request)
    {
        $raw_posts = BrowseRSSLatestNewsTask::handleSingle('https://hnrss.org/newest?q=ai', 240);

        foreach ($raw_posts as $raw_post) {
            dump($raw_post);
        }

    }

    public function notification(Request $request)
    {
        $post = Post::find(1);

        Notification::route('facebook', 'default')->notify(new PostWasPublished($post));
    }

    public function imageGen(Request $request)
    {
        $image_url = 'https://cdn.futurewalker.co/post_images_2/whats-next-for-openai-after-ceo-sam-altmans-ouster-1700439234754.jpg';

        $canvas = ImageGen::getOpenGraphImage($image_url, 'What’s Next for OpenAI After CEO Sam Altman’s Ouster What’s Next for OpenAI After CEO Sam Altman’s Ouster', '20 NOV • OPENAI • SAM ALTMAN • 3 min read ');

        return response($canvas->encode('jpeg'))
            ->header('Content-Type', 'image/jpeg');
    }

    public function indexing(Request $request)
    {
        $url = $request->input('url');

        if (is_null($url)) {
            abort(404);
        }

        IndexNow::submit($url);
        LaravelGoogleIndexing::create()->update($url);
    }
}
