<?php

namespace App\Http\Controllers\Auth;

use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use DB;


class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    public function authenticated(Request $request, $user)
    {
        if (empty($user->last_login_at))
        {
            $user->update([
                'last_login_at' => Carbon::now()->toDateTimeString()
            ]);
            return redirect('/new_user');
        }
        else
        {
            $user->update([
                'last_login_at' => Carbon::now()->toDateTimeString()
            ]);

            if($user->hasRole('admin')){
                return redirect('/admin/home');
            }
            else{
                return redirect('/player/home');
            }
             
        }
    }

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}
