<?php

namespace App\Http\Controllers\AdminAuth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Hesto\MultiAuth\Traits\LogsoutGuard;
use App\Models\Admin;
use App\Models\LoginRecords;
use Session;
use Browser;
use Carbon\Carbon;

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, LogsoutGuard {
        LogsoutGuard::logout insteadof AuthenticatesUsers;
    }

    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */
    public $redirectTo = '/admin/dashboard';

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

    /**
     * Show the application's login form.
     *
     * @return \Illuminate\Http\Response
     */
    public function showLoginForm()
    {
        return view('admin.auth.login');
    }

    public function login(Request $request)
    {
        $dateTime = date('y-m-d H:i:s',time());
        $admin = Admin::where('email',$request->email)->first();
        if(!$admin){
            return redirect('/admin')->with('error','Wrong Email/Password combination');
        }else{
            if($admin->status == '0'){
                return redirect('/admin')->with('error','Account is not Activated!');
            }else{
                $credentials = [
                    'email' => $request->email,
                    'password' => $request->password,
                    'status' => '1'
                ];

                if (Auth::guard('admin')->attempt($credentials)) {
                    $ip = $request->ip();
                    $browser = Browser::browserName();
                    $platform = Browser::platformName();
                    $device = Browser::deviceFamily();

                    $records = new LoginRecords;
                    $records->auth_type = 'Admin';
                    $records->auth_id = $admin->id;
                    $records->ip_address = $ip;
                    $records->browser = $browser;
                    $records->platform = $platform;
                    $records->device = $device;
                    $records->start_time = $dateTime;
                    $records->save();

                    Session::put('session_start', $dateTime);
                    Session::put('login_record', $records->id);
                    Session::put('admin_type', 'Admin');
                    return redirect('/admin/dashboard');
                }else{
                    return redirect('/admin')->with('error','Wrong Email/Password combination');
                }
            }
        }
    }

    /**
     * Get the guard to be used during authentication.
     *
     * @return \Illuminate\Contracts\Auth\StatefulGuard
     */
    protected function guard()
    {
        return Auth::guard('admin');
    }

    public function logout(){
        
        if(Session::get('login_record')){
            $record_id = Session::get('login_record');
            $time = date('y-m-d H:i:s',time());
            $records = LoginRecords::find($record_id);
            if($records){
                $records->end_time = $time;
                $records->update();
            }
        }
        Auth::guard('admin')->logout();

        return redirect('/admin');
    }
}
