<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Storage;
use App\Http\Requests\authUserRequest;
use App\Http\Requests\create_user_request;
use App\Models\User;
use App\Models\Abonent;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class UserController extends Controller
{
    public function reg(create_user_request $request)
    {
        $user = new User();
        $user->fio = $request->fioReg;
        $user->login = $request->loginReg;
        $user->number = $request->numberReg;
        $user->email = $request->emailReg;
        $user->password = $request->passReg;
        $user->save();
        Auth::login($user);
        return redirect()->route('index');
    }

    public function auth(authUserRequest $request)
    {
        if (Auth::attempt(['login' => $request->login,  'password' => $request->password])) {
            // $request->session()->regenerate();
            return redirect()->route('index');
            // return redirect()->intended('dashboard');
        }
        else {
            return redirect()->route('login')->withErrors([
                'login' => 'Incorrect password',
            ]);
        }
    }

    public function login() {
        return view('login');
    }

    public function register(){
        return view('login');
    }

    public function logout(User $user) {
        Auth::logout($user);
        return redirect()->route('index');
    }
}
