<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Admin;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class AuthAdminController extends Controller
{
    public function login()
    {
        if (auth('admin')->check()) {
            return redirect('/admin/home');
        }

        return view('admin.auth.login');
    }

    public function authenticate(Request $request)
    {

        $request->validate([
            'email' => 'required|email:filter',
            'password' => 'required|max:100',
        ]);

        $remember = $request->has('remember') ? true : false;
        if (Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password], $remember)) {

            return redirect('admin/home')->with('success', __('trans.auth.welcome_to', ['name' => auth('admin')->user()->name]));
        }

        return back()->with('error', __('trans.auth.error_login'))->withInput();
    }

    public function home()
    {
        return view('admin.home');
    }

    public function logout(Request $request)
    {
        Auth::guard('admin')->logout();

        $request->session()->invalidate();

        $request->session()->regenerateToken();

        return redirect('/admin/login');
    }
}
