<?php

namespace App\Http\Controllers\Web;

use Inertia\Inertia;
use App\Http\Controllers\Controller;
use App\Imports\ExportsImport;
use App\Models\Group;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Facades\Excel;

class HomeController extends Controller
{
    /**
     * Index
     */
    public function index()
    {
        return redirect()->route('exports');
    }

    /**
     * Lang
     */
    public function lang($lang)
    {
        session()->put('locale', $lang);

        return redirect()->back();
    }

    /**
     * Upgrade
     */
    public function upgrade()
    {
        Group::whereType('export')->get()
            ->each(function ($group) {
                collect($group->getOriginal('file'))->each(function ($file, $index) use ($group) {
                    if (Storage::exists("uploads/$file")) {
                        $id = now()->unix();
                        Excel::queueImport(new ExportsImport($id, $group, $index), storage_path("app/uploads/$file"));
                    }
                });
            });
        return 'done';
    }

    /**
     * Logout confirm page
     */
    public function logoutConfirm()
    {
        return view('logout');
    }
}
