<?php

namespace App\Http\Controllers\Vendor\Auth;

use App\Constants\Status;
use App\Http\Controllers\Controller;
use App\Lib\Intended;
use App\Models\AdminNotification;
use App\Models\UserLogin;
use App\Models\Vendor;
use Illuminate\Auth\Events\Registered;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rules\Password;

class RegisterController extends Controller {

    use RegistersUsers;

    public function __construct() {
        parent::__construct();
    }

    public function showRegistrationForm() {
        $pageTitle = "Register";
        Intended::identifyRoute();
        return view('vendor.auth.register', compact('pageTitle'));
    }

    protected function validator(array $data) {

        $passwordValidation = Password::min(6);

        if (gs('secure_password')) {
            $passwordValidation = $passwordValidation->mixedCase()->numbers()->symbols()->uncompromised();
        }

        $agree = 'nullable';
        if (gs('agree')) {
            $agree = 'required';
        }

        $validate = Validator::make($data, [
            'firstname' => 'required',
            'lastname'  => 'required',
            'email'     => 'required|string|email|unique:users',
            'password'  => ['required', 'confirmed', $passwordValidation],
            'captcha'   => 'sometimes|required',
            'agree'     => $agree,
        ], [
            'firstname.required' => 'The first name field is required',
            'lastname.required'  => 'The last name field is required',
        ]);

        return $validate;
    }

    public function register(Request $request) {
        if (!gs('vendor_registration')) {
            $notify[] = ['error', 'Registration not allowed'];
            return back()->withNotify($notify);
        }
        $this->validator($request->all())->validate();

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

        if (!verifyCaptcha()) {
            $notify[] = ['error', 'Invalid captcha provided'];
            return back()->withNotify($notify);
        }

        event(new Registered($vendor = $this->create($request->all())));

        $this->guard()->login($vendor);

        return $this->registered($request, $vendor)
        ?: redirect($this->redirectPath());
    }

    protected function create(array $data) {

        //User Create
        $vendor            = new Vendor();
        $vendor->email     = strtolower($data['email']);
        $vendor->firstname = $data['firstname'];
        $vendor->lastname  = $data['lastname'];
        $vendor->password  = Hash::make($data['password']);
        $vendor->kv        = gs('vendor_kv') ? Status::NO : Status::YES;
        $vendor->ev        = gs('ev') ? Status::NO : Status::YES;
        $vendor->sv        = gs('sv') ? Status::NO : Status::YES;
        $vendor->ts        = Status::DISABLE;
        $vendor->tv        = Status::ENABLE;
        $vendor->save();

        $adminNotification            = new AdminNotification();
        $adminNotification->user_id   = $vendor->id;
        $adminNotification->title     = 'New member registered';
        $adminNotification->click_url = urlPath('admin.users.detail', $vendor->id);
        $adminNotification->save();

        //Login Log Create
        $ip          = getRealIP();
        $exist       = UserLogin::where('user_ip', $ip)->first();
        $vendorLogin = new UserLogin();

        if ($exist) {
            $vendorLogin->longitude    = $exist->longitude;
            $vendorLogin->latitude     = $exist->latitude;
            $vendorLogin->city         = $exist->city;
            $vendorLogin->country_code = $exist->country_code;
            $vendorLogin->country      = $exist->country;
        } else {
            $info                      = json_decode(json_encode(getIpInfo()), true);
            $vendorLogin->longitude    = @implode(',', $info['long']);
            $vendorLogin->latitude     = @implode(',', $info['lat']);
            $vendorLogin->city         = @implode(',', $info['city']);
            $vendorLogin->country_code = @implode(',', $info['code']);
            $vendorLogin->country      = @implode(',', $info['country']);
        }

        $vendorAgent          = osBrowser();
        $vendorLogin->user_id = $vendor->id;
        $vendorLogin->user_ip = $ip;

        $vendorLogin->browser = @$vendorAgent['browser'];
        $vendorLogin->os      = @$vendorAgent['os_platform'];
        $vendorLogin->save();

        return $vendor;
    }

    public function checkVendor(Request $request) {
        $exist['data'] = false;
        $exist['type'] = null;
        if ($request->email) {
            $exist['data']  = Vendor::where('email', $request->email)->exists();
            $exist['type']  = 'email';
            $exist['field'] = 'Email';
        }
        if ($request->mobile) {
            $exist['data']  = Vendor::where('mobile', $request->mobile)->where('dial_code', $request->mobile_code)->exists();
            $exist['type']  = 'mobile';
            $exist['field'] = 'Mobile';
        }
        if ($request->username) {
            $exist['data']  = Vendor::where('username', $request->username)->exists();
            $exist['type']  = 'username';
            $exist['field'] = 'Username';
        }
        return response($exist);
    }

    public function registered() {
        return to_route('vendor.home');
    }

    protected function guard() {
        return Auth::guard('vendor');
    }
}
