<?php

declare(strict_types=1);

namespace Seka\Portal\Modul\Statistiky\Console;

use Carbon\Carbon;
use Illuminate\Console\Command;
use Seka\Portal\Modul\Statistiky\Entities\Import\Import;
use Seka\Portal\Modul\Statistiky\Exceptions\ZiadnyImportZaznamovZaPredchadzajuciDenException;

/**
 * Class KontrolaImportuZaznamovCommand
 *
 * @package Seka\Portal\Modul\Statistiky\Console
 */
final class KontrolaImportuZaznamovCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'statistiky:kontrola-importu {datum : Datum importu vo formate Y-m-d}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Kontrola prebehnutia importu statistik technikov a pracovisk emisnych kontrol za dany den';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @return int
     * @throws \Seka\Portal\Modul\Statistiky\Exceptions\ZiadnyImportZaznamovZaPredchadzajuciDenException
     */
    public function handle(): int
    {
        $date = Carbon::createFromFormat('Y-m-d', $this->argument('datum'));

        $result = 0 < Import::query()
                ->whereDate('datum_cas_akcie', $date)
                ->where('subor_velkost', '>', 0)
                ->get()
                ->count();

        if (!$result) {
            throw new ZiadnyImportZaznamovZaPredchadzajuciDenException(
                "Za den {$date->format('d.m.Y')} sa nenasiel ziadny import zaznamov zo suboru."
            );
        }

        return 1;
    }
}
