<?php

namespace App\Console;

use App\Facades\Options;
use App\Jobs\HeartBeat;
use App\Jobs\SAP\SyncSapPricesJob;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Mail;

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

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $cronTasks = Options::get('cron_tasks');

        if ($cronTasks['sync_sap_products']) {
            $schedule->command('area:sync-sap-products')
                ->everyThirtyMinutes()
                ->between('01:00', '01:40')
                ->withoutOverlapping()
                ->onFailure(function () {
                    $cacheKey = 'alert_sent_sync_sap_products';

                    if (!Cache::has($cacheKey)) {
                        Mail::to(explode(',', config('mail.alerts_to')))
                            ->send(new \App\Mail\AlertNotification(
                                'Scheduled Command Failed: area:sync-sap-products'
                            ));

                        Cache::put($cacheKey, true, now()->addDay());
                    }

                    if (Cache::has('control_products_integration')) {
                        Cache::forget('control_products_integration');
                    }
                })->onSuccess(function () {
                    Cache::put('control_products_integration', true, now()->addDay());
                });
        }
        if ($cronTasks['sync_sap_prices']) {
            $schedule->command('area:sync-sap-prices')
                ->everyFifteenMinutes()
                ->unlessBetween('01:00', '01:40')
                ->withoutOverlapping()
                ->onFailure(function () {
                    $cacheKey = 'alert_sent_sync_sap_prices';

                    if (!Cache::has($cacheKey)) {
                        Mail::to(explode(',', config('mail.alerts_to')))
                            ->send(new \App\Mail\AlertNotification(
                                'Scheduled Command Failed: area:sync-sap-prices'
                            ));

                        Cache::put($cacheKey, true, now()->addDay());
                    }

                    if (Cache::has('control_prices_integration')) {
                        Cache::forget('control_prices_integration');
                    }
                })->onSuccess(function () {
                    Cache::put('control_prices_integration', true, now()->addHour());
                });
        }
        if ($cronTasks['check_orders_sap']) {
            $schedule->command('area:check-orders-sap')
                ->hourly()
                ->withoutOverlapping();
        }
        if ($cronTasks['sync_shopify_products']) {
            $schedule->command('area:sync-shopify-products')
                ->everyMinute()
                ->between('02:00', '08:00')
                ->withoutOverlapping();
        }
        if ($cronTasks['sync_shopify_prices']) {
            $schedule->command('area:sync-shopify-prices')
                ->everyFiveMinutes()
                ->withoutOverlapping();
        }
        if ($cronTasks['sync_shopify_collections']) {
            $schedule->command('area:sync-shopify-collections')
                ->dailyAt('09:00')
                ->withoutOverlapping();
        }
        if ($cronTasks['check_pending_orders']) {
            $schedule->command('area:check-expired-orders')
                ->everyFifteenMinutes()
                ->between('05:00', '07:00')
                ->withoutOverlapping();
        }
        /*
        if ($cronTasks['event_reg_process_list']) {
            $schedule->command('area:generate-qrcodes')
                ->everyThreeMinutes()
                ->withoutOverlapping();
        }
        */
        $schedule->job(HeartBeat::class)
            ->everyFifteenMinutes()
            ->withoutOverlapping();
    }

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

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