<?php

namespace App\Http\Controllers;

use App\Http\Helpers\UserSystemInfoHelper;
use Illuminate\Http\Request;
use App\Models\{Utilisateur, Book, Domaine, Book_Utilisateur, Etablissement};

class IndexController extends Controller
{
    public function create()
    {
        if (session()->has('BOOKCASE_USER')) return redirect()->route('user');

        $namebase = Etablissement::where('idEtablissement', 11)->first()->namebase;
        UserSystemInfoHelper::changeEnv(['database' => $namebase]);

        $stats = [
            'user' => Utilisateur::all()->count() - 1,
            'livre' => Book::where('etat', 1)->count(),
            'emprunt' => Book_Utilisateur::all()->count()
        ];
        $etablissement = Etablissement::orderBy('sigle')->get(['idEtablissement', 'sigle']);
        $domaines = Domaine::with('disciplines')->get();
        return view('index', compact('stats', 'domaines', 'etablissement'));
    }
}
