<?php

declare(strict_types=1);

namespace Seka\Portal\Modul\Statistiky\Http\Controllers\Backend;

use Exception;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Log;
use Seka\Portal\Modul\Statistiky\Http\Controllers\Controller;

/**
 * Class SpustitImport
 *
 * @package Seka\Portal\Modul\Statistiky\Http\Controllers\Backend
 */
final class SpustitImport extends Controller
{
    /**
     * @return \Illuminate\Http\RedirectResponse
     */
    public function __invoke(): RedirectResponse
    {
        try {
            Artisan::queue('statistiky:import', [
                'cesta' => 'storage/app/sftp/statistiky',
                'presunutdo' => 'storage/app/sftp/statistiky/importovane',
                '--pocet' => 1,
            ])->onQueue('long-time-job-queue');
        } catch (Exception $e) {
            Log::error($e->getMessage(), $e->getTrace());

            flash()->error('Chyba pri spustení importu.');
        } finally {
            return back();
        }
    }
}
