<?php

use App\Helpers\FirstParty\OpenAI\OpenAI;
use App\Jobs\AISerpGenArticleJob;
use App\Jobs\GenerateArticleFeaturedImageJob;
use App\Jobs\GenerateArticleJob;
use App\Jobs\Tasks\GetNewsSerpTask;
use App\Jobs\Tasks\ParseNewsSerpDomainsTask;
use App\Jobs\Tasks\ScrapeUrlBodyTask;
use App\Models\Category;
use App\Models\NewsSerpResult;
use App\Models\Post;
use App\Models\SerpUrl;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

// use LaravelFreelancerNL\LaravelIndexNow\Facades\IndexNow;
// use LaravelGoogleIndexing;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/now-minute', function (Request $request) {
    dd(now()->minute);
});

Route::get('/indexing', [App\Http\Controllers\Tests\TestController::class, 'indexing']);

Route::get('/serp-ai-gen', function (Request $request) {
    AISerpGenArticleJob::dispatch()->onQueue('default')->onConnection('default');
});

Route::get('/exponential', function (Request $request) {
    $post_counts = get_exponential_posts_gen_by_day($request->input('day', 1));

    dump('Day: '.$request->input('day', 1));
    dump('Post Counts: '.$post_counts);
});

Route::get('/step-1', function (Request $request) {
    $category = Category::find($request->input('id'));
    $news_serp_result = GetNewsSerpTask::handle($category, 'US');
    dd($news_serp_result->id);
});

Route::get('/step-2', function (Request $request) {
    $news_serp_result = NewsSerpResult::find($request->input('id', null));

    if (is_null($news_serp_result)) {
        abort(404);

    }
    $task = ParseNewsSerpDomainsTask::handle($news_serp_result);

    if ($task) {
        $serp_url = SerpUrl::latest()->first();

        dd($serp_url->id);
    }
});

Route::get('/step-3', function (Request $request) {
    $serp_url = SerpUrl::find($request->input('id', null));

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

    $task = GenerateArticleJob::dispatch($serp_url)->onQueue('default')->onConnection('default');

    dd($task);
});

Route::get('/step-4', function () {
    $post = Post::whereNull('featured_image')->where('status', 'draft')->first();

    $task = GenerateArticleFeaturedImageJob::dispatch($post)->onQueue('default')->onConnection('default');

    dd($task);
});

Route::get('/step-5', function (Request $request) {
    $post = Post::find($request->input('id'));

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

    $post->published_at = now();
    dd($post->save());
});

// Route::get('/suggest_titles', function () {
//     $results = OpenAI::suggestArticleTitles("It's 2019s Electric: How Fisker Is Reinventing The Automotive Industry And \nExpanding Its Business", "Fisker's approach to building electric vehicles is deeply intertwined with \nits overall business philosophy: use less, use better,...s", 1);
//     dd($results);
// });

// Route::get('/write_article_raw', function () {
//     $results = OpenAI::writeArticle("Fisker's Vision for the Future of Electric Cars", "Explore Fisker's innovative vision for the future of electric cars and its impact on the automotive industry.", 'Article', 500, 800);
//     dd($results);
// });

Route::get('proxy_test', function () {
    $url = 'https://www.cnbc.com/2023/09/24/this-southern-city-is-the-no-1-place-to-start-your-own-business.html';

    $task = ScrapeUrlBodyTask::handle($url);

    dd($task);
});

// Route::get('/image_gen', function() {
//   $post =
//   return GenerateArticleFeaturedImageTask::handle("","","","");
// });
