<?php

namespace App\Http\Controllers\Admin\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Brian2694\Toastr\Facades\Toastr;
use Gregwar\Captcha\CaptchaBuilder;
use Illuminate\Support\Facades\Session;
use App\CentralLogics\Helpers;
use App\Models\Admin;
use App\Models\Module;
use Illuminate\Support\Facades\Http;

class LoginController extends Controller
{
    public function __construct()
    {
        $this->middleware('guest:admin', ['except' => 'logout']);
    }

    public function login()
    {
        $custome_recaptcha = new CaptchaBuilder;
        $custome_recaptcha->build();
        Session::put('six_captcha', $custome_recaptcha->getPhrase());
        return view('admin-views.auth.login', compact('custome_recaptcha'));
    }

    public function submit(Request $request)
    {
        $request->validate([
            'email' => 'required|email',
            'password' => 'required|min:6'
        ]);

//        $recaptcha = Helpers::get_business_settings('recaptcha');
//        if (isset($recaptcha) && $recaptcha['status'] == 1) {
//            $request->validate([
//                'g-recaptcha-response' => [
//                    function ($attribute, $value, $fail) {
//                        $secret_key = Helpers::get_business_settings('recaptcha')['secret_key'];
//                        $response = $value;
//                        $url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . $secret_key . '&response=' . $response;
//                        $response = Http::get($url);
//                        $response = $response->json();
//                        if (!isset($response['success']) || !$response['success']) {
//                            $fail(translate('messages.ReCAPTCHA Failed'));
//                        }
//                    },
//                ],
//            ]);
//        } else if(strtolower(session('six_captcha')) != strtolower($request->custome_recaptcha))
//        {
//            Toastr::error(translate('messages.ReCAPTCHA Failed'));
//            return back();
//        }

        if (auth('admin')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember)) {
            $admin = Admin::find(auth('admin')->id());
            $admin->is_logged_in = 1;
            $admin->save();
            $modules = Module::Active()->get();
            if(isset($modules)&&($modules->count()>0)){

                return redirect()->route('admin.dashboard');
            }
            return redirect()->route('admin.business-settings.business-setup');
        }

        return redirect()->back()->withInput($request->only('email', 'remember'))
            ->withErrors(['Credentials does not match.']);
    }

    public function logout(Request $request)
    {
        auth()->guard('admin')->logout();
        return redirect()->route('admin.auth.login');
    }
}
