<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Illuminate\Support\Facades\Validator;
// use Illuminate\Support\Facades\Session;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

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
     */
    protected $redirectTo = '/';

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

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

    public function validateLogin(Request $request)
    {
        Validator::make($request->all(), [
            $this->username() => 'required|string',
            // 'password' => ['required', 'regex:/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{9,}$/'],
        ], [
            // 'password.min' => ':attribute باید حداقل :min کاراکتر باشد.',
        ], [
            // $this->username() => 'نام',
            // 'password' => 'رمز عبور',
        ])->validate();
    }

    public function sendFailedLoginResponse(Request $request)
    {
        throw ValidationException::withMessages([
            'auth.failed' => [trans('auth.failed')],
        ]);
    }

    /**
     * The user has logged out of the application.
     *
     * @return mixed
     */
    protected function loggedOut()
    {
        return redirect('/');
    }

    public function username()
    {
        return 'username';
    }

    protected function authenticated($request, $user)
    {
        // Session::put('last_login', now());
        if (!$request->session()->exists('last_login')) {
            $request->session()->put('last_login', now());
        }
        $user->last_login = now();
        $user->save();
    }

    public function logout(Request $request)
    {
        /** Hamid edits */
        if($request->session()->exists('last_login')){
            // dd(auth()->user());

            $user = auth()->user();
            $user->last_login = $request->session()->get('last_login');
            $user->save();
            // auth()->user()->update([    
            //     'last_login' => $request->session()->get('last_login')
            // ]);

            $request->session()->forget('last_login');
        }

        $this->guard()->logout();

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

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

        return $this->loggedOut($request) ?: redirect('/');
    }
}