<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use App\Jobs\ProcessEndingFavouriteNotifications;
use App\Jobs\ProcessEndingTomorrowFavouriteArtistNotifications;
use App\Jobs\ProcessOpeningTomorrowFavouriteArtistNotifications;
use App\Jobs\ProcessOpeningTomorrowFavouriteNotifications;
use App\Jobs\ProcessNewExhibitFavouriteArtistNotifications;
use App\Jobs\ProcessWeeklyOpeningNotifications;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Stringable;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
    ];

    /**
     * Get the timezone that should be used by default for scheduled events.
     *
     * @return \DateTimeZone|string|null
     */
    protected function scheduleTimezone()
    {
        return 'Europe/Lisbon';
    }

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {


        //Clear expired password reset requests
        $schedule->command('auth:clear-resets')->everySixHours();


        $schedule->job(new ProcessEndingFavouriteNotifications)
            ->dailyAt('10:00')
            ->after(function () {
                Log::info('Ending favourites job executed');
            })
            ->onFailure(function (Stringable $output) {
                Log::error('Ending favourites failed:' . $output->__toString());
            });

        $schedule->job(new ProcessEndingTomorrowFavouriteArtistNotifications)
            ->dailyAt('10:00')
            ->after(function () {
                Log::info('Ending tomorrow favourites job executed');
            })->onFailure(function (Stringable $output) {
                Log::error('Ending tomorrow favourites job  failed:' . $output->__toString());
            });

        $schedule->job(new ProcessOpeningTomorrowFavouriteArtistNotifications)
            ->dailyAt('10:00')
            ->after(function () {
                Log::info('Opening tomorrow fabourite artists job executed');
            })
            ->onFailure(function (Stringable $output) {
                Log::error('Opening tomorrow fabourite artists job failed:' . $output->__toString());
            });


        $schedule->job(new ProcessNewExhibitFavouriteArtistNotifications)
            ->dailyAt('10:00')
            ->after(function () {
                Log::info('Opening tomorrow fabourite artists job executed');
            })
            ->onFailure(function (Stringable $output) {
                Log::error('Opening tomorrow fabourite artists job failed:' . $output->__toString());
            });

        $schedule->job(new ProcessOpeningTomorrowFavouriteNotifications)
            ->dailyAt('10:00')
            ->after(function () {
                Log::info('Opening tomorrow favourites job executed');
            })
            ->onFailure(function (Stringable $output) {
                Log::error('Opening tomorrow favourites job failed:' . $output->__toString());
            });


        $schedule->job(new ProcessWeeklyOpeningNotifications)
            //->everyMinute()
            ->weeklyOn(1, '12:05')
            ->after(function () {
                Log::info('Weekly openings job executed');
            })
            ->onFailure(function (Stringable $output) {
                Log::error('Weekly openings job executed failed:' . $output->__toString());
            });
    }

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

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