<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class Kernel extends ConsoleKernel
{
    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        /*
            Cara kerja scheduler Laravel:
            1. Kamu definisikan schedule di app/Console/Kernel.php.
            2. Laravel tidak akan menjalankan sendiri schedule itu.
            3. Harus ada cron job (atau php artisan schedule:work) yang memicu Laravel setiap menit.

            keterangan :
            -> runInBackground : Kalau 2 job jalan di jam yang sama, Laravel akan jalankan berurutan (bukan paralel). Kalau butuh jalan paralel, bisa tambahkan ->runInBackground():
            -> withoutOverlapping : Kalau sebuah job masih berjalan (belum selesai) ketika jadwal berikutnya datang, Laravel tidak akan memulai job baru sampai job lama selesai.
            
            list:
            ->everyMinute() → tiap menit
            ->everyTwoMinutes()
            ->everyThreeMinutes()
            ->everyFourMinutes()
            ->everyFiveMinutes()
            ->everyTenMinutes()
            ->everyFifteenMinutes()
            ->everyThirtyMinutes()
            $schedule->command('job:deposit_notify')->dailyAt('08:00');  jam 8 pagi
            $schedule->command('job:deposit_notify')->hourly()->runInBackground(); //paralel
        */
        $schedule->command('job:send_mail')->everyFiveMinutes()->withoutOverlapping()->after(function () {
            Log::channel('schedule_laravel')->info('Execute job:send_mail selesai.');
        });
        
        $schedule->command('job:send_system_errors')->hourly()->withoutOverlapping()->after(function () {
            Log::channel('schedule_laravel')->info('Execute job:send_system_errors selesai.');
        });
  
        // Clean expired sessions daily at 3 AM
        $schedule->command('sessions:clean')->dailyAt('03:00')->withoutOverlapping()->after(function () {
            Log::channel('schedule_laravel')->info('Execute sessions:clean selesai.');
        });

        // Clear expired user sessions every 30 minutes
        $schedule->command('sessions:clear-expired')->everyThirtyMinutes()->withoutOverlapping()->after(function () {
            Log::channel('schedule_laravel')->info('Execute sessions:clear-expired selesai.');
        });

        // Clean old uploaded Excel files daily at 4 AM (older than 30 days)
        $schedule->command('excel:cleanup')->dailyAt('04:00')->withoutOverlapping()->after(function () {
            Log::channel('schedule_laravel')->info('Execute excel:cleanup selesai.');
        });

        // Unlock Stock Opname documents that have been locked for more than 3 minutes
        $schedule->command('stockopname:unlock')->everyTwoMinutes()->withoutOverlapping()->after(function () {
            Log::channel('schedule_laravel')->info('Execute stockopname:unlock selesai.');
        });
    }

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

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