<?php

namespace App\Http\Controllers\Agent\Auth;

use App\Http\Controllers\Controller;
use App\Models\Telemedpro;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

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 = '/agent';

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

    public function showLoginForm()
    {
        return view('agent.auth.login');
    }

    protected function login(Request $request)
    {
        $credentials = $request->only('email', 'password');

        $user = Telemedpro::where($this->username(), $credentials['email'])->first();

        if ($user && Hash::check($credentials['password'], $user->password)) {
            // Auth::guard('agent')->login($user, $request->has('remember'));
            if ($this->guard('agent')->attempt(
                $this->credentials($request),
                $request->has('remember')
            )) {
                // dd($this->guard());
                return redirect($this->redirectTo);
            }
            dd($this->guard());
            // dd($user && Hash::check($credentials['password'], $user->password));
            return redirect($this->redirectTo);
        }

        return back()->withErrors(['email' => 'Invalid credentials']);
    }

    public function redirectPath()
    {
        return "/agent";
    }
    public function loginAgent(Request $request)
    {
        $validatedData = $request->validate([
            'email' => 'required|email',
            'password' => 'required'
        ]);

        $patient = Telemedpro::where('email', $validatedData['email'])->first();

        if (!$patient || !Hash::check($validatedData['password'], $patient->password)) {
            return response()->json([
                'message' => 'Invalid credentials'
            ], 422);
        }
        if (!$patient || $patient->status == 0) {
            return response()->json([
                'message' => 'Your account is undergoing verification.'
            ], 422);
        }
        $token = $patient->createToken('auth_token')->plainTextToken;

        return response()->json([
            'data' => $patient,
            'access_token' => $token,
            'token_type' => 'Bearer',
        ]);
    }
}
