<?php

use App\Helpers\FirstParty\OpenAI\OpenAI;
use App\Jobs\AISerpGenArticleJob;
use App\Jobs\BrowseAndWriteWithAIJob;
use App\Jobs\BrowseRSSPostJob;
use App\Jobs\BrowseSingleRSSJob;
use App\Jobs\CrawlRssPostJob;
use App\Jobs\FillPostMetadataJob;
use App\Jobs\GenerateArticleFeaturedImageJob;
use App\Jobs\GenerateArticleJob;
use App\Jobs\SaveOldKeywordsJob;
use App\Jobs\Tasks\GetNewsSerpTask;
use App\Jobs\Tasks\ParseNewsSerpDomainsTask;
use App\Jobs\Tasks\ScrapeUrlBodyTask;
use App\Jobs\Tasks\WriteWithAITask;
use App\Models\Category;
use App\Models\NewsSerpResult;
use App\Models\Post;
use App\Models\RssPost;
use App\Models\SerpUrl;
use App\Notifications\PostIncomplete;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

//use Notification;

// 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('/seed', function (Request $request) {

    $rss_url = $request->input('rss_url');

    if (is_empty($rss_url)) {
        return abort(404);
    }

    BrowseSingleRSSJob::dispatch($rss_url, 2880)->onQueue('default')->onConnection('default');

});

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

Route::get('/set_domain', function (Request $request) {

    $last_record = RssPost::whereNull('post_domain')->orderBy('id', 'DESC')->first();

    dd($last_record);

    for ($i = 1; $i <= $last_record->id; $i++) {
        $rss_post = RssPost::find($i);

        if (! is_null($rss_post)) {
            $rss_post->post_domain = get_domain_from_url($rss_post->post_url);
            $rss_post->save();
        }
    }

    return 'ok';
});

Route::get('/set_keywords', function (Request $request) {

    $last_record = RssPost::where('keyword_saved', false)->orderBy('id', 'DESC')->first();

    for ($i = 1; $i <= $last_record->id; $i++) {
        SaveOldKeywordsJob::dispatch($i)->onQueue('default')->onConnection('default');
    }

    return 'ok';
});

Route::get('/recrawl_from', function (Request $request) {

    $id = $request->input('id');

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

    $last_record = RssPost::orderBy('id', 'DESC')->first();

    for ($i = $id; $i < $last_record->id; $i++) {
        CrawlRssPostJob::dispatch($i)->onQueue('default')->onConnection('default');
    }

    return 'ok';
});

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

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

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

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

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

Route::get('/fire_rss', function (Request $request) {

    $hours = $request->input('hours');

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

    BrowseRSSPostJob::dispatch($hours)->onQueue('default')->onConnection('default');
});

Route::get('/incomplete/post', function (Request $request) {

    $post = Post::find(1);

    Notification::route(get_notification_channel(), get_notification_user_id())->notify(new PostIncomplete($post));
});

Route::get('/fire/now', function (Request $request) {
    BrowseAndWriteWithAIJob::dispatch()->onQueue('default')->onConnection('default');
});

Route::get('/aj/oooi', function (Request $request) {

    WriteWithAITask::handle(1);

    //return view('welcome');
});

Route::get('/aj/curj', function (Request $request) {

    FillPostMetadataJob::dispatch($request->input('id', 1))->onQueue('default')->onConnection('default');

    //return view('welcome');
});

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("","","","");
// });
