<?php

namespace App\Http\Controllers;

use App\Models\User;
use Auth;
use Brian2694\Toastr\Facades\Toastr;
use Illuminate\Http\Request;

class AuthController extends Controller
{
    public function index(Request $request)
    {
        if ($request->user() != null) return redirect()->route('admin.dashboard');
        if ($request->route()->getName() == 'admin.login') {
            return view('admin.login');
        }
        if ($request->route()->getName() != 'admin.login') {
            Toastr::info('You aren\'t Logged in');
            return redirect()->route('admin.login');
        }
    }

    public function adminLogin(Request $request)
    {
        $credetials = $request->only('email', 'password');
        if (User::where('email', $request->email)->first()->id == 1 && Auth::attempt($credetials)) {
            return redirect()->route('admin.dashboard');
        }
        Toastr::error('Invalid Credentials');
        return redirect()->route('admin.login');
    }

    public function adminLogout(Request $request)
    {
        auth()->logout();
        Toastr::success('Logged out successful');
        return redirect()->route('admin.login');
    }
}
