<?php

namespace App\Console;

use App\Jobs\AISerpGenArticleJob;
use Carbon\Carbon;
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
    {

        // AI Gen Scheduler

        $launched_date = Carbon::parse(intval(config('platform.global.launched_epoch')));
        $days_since_launch = now()->diffInDays($launched_date) + 1;
        $posts_to_generate = get_exponential_posts_gen_by_day($days_since_launch);
        $mins_between_posts = floor((24 * 60) / $posts_to_generate);

        $schedule->call(function () {
            AISerpGenArticleJob::dispatch()->onQueue('default')->onConnection('default');
        })->everyMinute()->when(function () use ($mins_between_posts, $launched_date) {
            $minutes_since_launch = now()->diffInMinutes($launched_date);

            return $minutes_since_launch % $mins_between_posts === 0;
        });

    }

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

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