<?php

namespace App\Console;

use App\Jobs\GetUrlBodyJob;
use App\Jobs\ShopeeSellerTopProductScraperJob;
use App\Models\Category;
use App\Models\DailyTaskSchedule;
use App\Models\ShopeeSellerCategory;
use App\Models\UrlToCrawl;
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)
    {
        $schedule->call(function () {
          $url_to_crawl = UrlToCrawl::where('is_crawling', false)->inRandomOrder()->first();

          if (!is_null($url_to_crawl))
          {
            GetUrlBodyJob::dispatch($url_to_crawl->id)->onQueue('default')->onConnection('default');
          }

        })->everyTenMinutes()->name('parse-url-every-10m');
    }

    /**
     * Schedule a category.
     */
    private function scheduleCategory(Schedule $schedule, $locale, $categoryName, $taskName)
    {
        $schedule->call(function () use ($locale, $categoryName) {

            $category = Category::where('country_locale_slug', $locale)->where('name', $categoryName)->first();

            if (! is_null($category)) {
                $task = $category->country_locale_slug.'-'.$category->slug;

                $nextRun = DailyTaskSchedule::where('task', $task)->first();

                if (! is_null($nextRun)) {
                    $currentTime = now();

                    if ($currentTime->gte($nextRun->next_run_time)) {

                        $shopee_seller_category = ShopeeSellerCategory::where('category_id', $category->id)
                            ->orderByRaw('ISNULL(last_ai_written_at) DESC')
                            ->orderBy('last_ai_written_at', 'asc')
                            ->orderBy('id', 'asc')
                            ->first();

                        $task = ShopeeSellerTopProductScraperJob::dispatch($shopee_seller_category->seller, $category->country_locale->country_iso, $category)
                            ->onQueue('default')
                            ->onConnection('default');

                        $nextRun->next_run_time = now()->addMinutes(rand(1200, 1440));
                        $nextRun->save();
                    }
                } else {
                    $nextRun = new DailyTaskSchedule;
                    $nextRun->task = $category->country_locale_slug.'-'.$category->slug;
                    $nextRun->next_run_time = now()->addMinutes(rand(0, 1440));
                    $nextRun->save();
                }
            }

        })->everyMinute()->name($taskName);
    }

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

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