<?php

declare(strict_types=1);

namespace Seka\Portal\Modul\Statistiky\Providers;

use Carbon\Carbon;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\ServiceProvider;
use Seka\Portal\Modul\Statistiky\Console\ImportZaznamovCommand;
use Seka\Portal\Modul\Statistiky\Console\KontrolaImportuZaznamovCommand;

/**
 * Class ScheduleServiceProvider
 *
 * @package Seka\Portal\Modul\Statistiky\Providers
 */
final class ScheduleServiceProvider extends ServiceProvider
{
    /**
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function boot(): void
    {
        $this->callAfterResolving(Schedule::class, function (Schedule $schedule) {
            $schedule->command(
                ImportZaznamovCommand::class,
                ['storage/app/sftp/statistiky', 'storage/app/sftp/statistiky/importovane']
            )
                ->everyTwoHours()
                ->withoutOverlapping(10)
                ->onOneServer()
                ->runInBackground()
                ->emailOutputOnFailure(config('statistiky.import_email_address'));

            $schedule->command(
                KontrolaImportuZaznamovCommand::class,
                [Carbon::now()->subDay()->format('Y-m-d')]
            )
                ->onOneServer()
                ->runInBackground()
                ->dailyAt('08:00')
                ->emailOutputOnFailure(config('statistiky.import_email_address'));
        });
    }
}
