<?php

namespace App\Console;

use App\Jobs\BrowseDFSAndWriteWithAIJob;
use App\Jobs\BrowseRSSPostJob;
use App\Jobs\PublishIndexPostJob;
use App\Jobs\PublishRssSearchResultJob;
use App\Models\HybridTopRssPostKeywords;
use App\Models\Post;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * Define the application's command schedule.
     */
    protected function schedule(Schedule $schedule): void
    {
        $schedule->command('sitemap:generate')->everySixHours()->name('sitemap-generate-every-six-hours');

        $schedule->call(function () {
            BrowseRSSPostJob::dispatch(1)->onQueue('default')->onConnection('default');
        })->hourly()->name('browse-rss-post-job-hourly');

        $schedule->call(function () {

            $top_rss_keywords = HybridTopRssPostKeywords::get(1, 20);

            foreach ($top_rss_keywords as $rss_keyword) {
                PublishRssSearchResultJob::dispatch($rss_keyword->value_lowercased)->onQueue('default')->onConnection('default');
            }

        })->twiceDaily(0, 12)->name('top-rss-keywords-indexing-daily');

        // $schedule->call(function () {
        //     BrowseDFSAndWriteWithAIJob::dispatch()->onQueue('default')->onConnection('default');
        // })->everySixHours()->name('write-a-job-6hrs');

        // $schedule->call(function () {
        //     $future_post = Post::whereNotNull('published_at')->where('status', 'future')->where('published_at', '<=', now())->orderBy('published_at', 'ASC')->first();

        //     if (! is_null($future_post)) {
        //         PublishIndexPostJob::dispatch($future_post->id)->onQueue('default')->onConnection('default');
        //     }

        // })->everyMinute()->name('schedule-future-post');

    }

    /**
     * Register the commands for the application.
     */
    protected function commands(): void
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}
