<?php namespace App\Services;

use App\User;
use Validator;
use Illuminate\Contracts\Auth\Registrar as RegistrarContract;
use App\Traits\CaptchaTrait;

class Registrar implements RegistrarContract {
	use CaptchaTrait;
	
	/**
	 * Get a validator for an incoming registration request.
	 *
	 * @param  array  $data
	 * @return \Illuminate\Contracts\Validation\Validator
	 */
	public function validator(array $data)
	{
		

		$data['captcha'] = $this->captchaCheck();

		return Validator::make($data, [
			'name' => 'required|max:255',
			'username' => 'required|max:255|unique:user_logins',
			'email' => 'required|email|max:255|unique:user_logins',
			'password' => 'required|confirmed|min:6',
			'g-recaptcha-response'  => 'required',
            'captcha'               => 'required|min:1'
		],
		[
			'g-recaptcha-response.required' => 'Captcha is required',
        	'captcha.min'           		=> 'Wrong captcha, please try again.'
		]);
	}

	/**
	 * Create a new user instance after a valid registration.
	 *
	 * @param  array  $data
	 * @return User
	 */
	public function create(array $data)
	{
		return User::create([
			'name' => $data['name'],
			'username' => $data['username'],
			'email' => $data['email'],
			'password' => bcrypt($data['password']),
			'role' => 'user'
		]);
	}

}
