<?php

namespace App\Http\Controllers\Auth;

use App\Invitation;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\DB;


class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;


    /**
     * Where to redirect users after registration.
     *
     */
    protected $redirectTo = '/home/1';

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

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {

        $messages = array(
            'name.required' => 'El nombre es requerido',
            'email.required' => 'El campo Email del responsable del Área es requerido.',
            'email.email'=>'Ingrese un E-Mail válido.',
            'email.unique'=>'Existe un usuario registrado con el E-Mail ingresado.',
            'email.regex'=>'Ingrese un E-Mail con dominio untref ó bienalsur.',
            'password.required' =>'El campo contraseña es requerido',
            'password.min' =>'Contraseña debe tener 6 caracteres mínimo.',
            'password_confirmation.required' =>'El campo Confirmar Contraseña es requerido.',
            'password_confirmation.min' =>'Confirmar Contraseña debe tener 6 caracteres mínimo.',
            'password_confirmation.confirmed' =>'El campo Confirmar Contraseña no coincide con Contraseña.'

        );
        return Validator::make($data, [
            'name' => 'required|string|max:255',
//            'email' => 'required|string|email|max:255|unique:users',
            'email'=>['required','email','unique:users','regex:/[a-zA-z0-9]+((\@untref\.edu\.ar)|(\@bienalsur\.org))$/u'],
            'password' => 'required|string|min:6|confirmed',
        ],$messages);
    }



    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {


            $user = User::create([
                'name' => $data['name'],
                'email' => $data['email'],
                'password' => bcrypt($data['password']),
            ]);


            if ($data['code']) {

                $profile = Invitation::where('token', '=', $data['code'])->get();


                DB::table('profile_user')->insert(
                    ['profile_id' => $profile[0]->profile_id, 'user_id' => $user->id]
                );


                //  El usuario invitado se registro

                $Invitation = Invitation::where('profile_id', '=', $profile[0]->profile_id)->where('email', '=', $profile[0]->email)->where('token', '=', $data['code'])->first();

                $Invitation->status = '1';
                $Invitation->save();
            }

            return $user;


        }

}
