<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\User;
use Image;
use Auth;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use Mail;

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.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * 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)
    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255', 'unique:users', 'alpha_dash'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'user_data.first_name' => ['required', 'string', 'max:255'],
            'user_data.last_name' => ['required', 'string', 'max:255'],
            'user_data.phone' => ['required', 'string', 'max:255'],
            'password' => ['required', 'string', 'min:6', 'confirmed'],
        ], [
            'name.unique' => 'El nombre de usuario ya ha sido registrado',
            'email.unique' => 'La dirección de email ya ha sido registrada',
        ]);
    }

    protected function registered(Request $request, $user)
    {
        if(!$user->active) {
            Auth::logout();
            return redirect('login')->with('status', 'Gracias por registrarte. Te notificaremos cuando tu cuenta esté activa');
        }
    }

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

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data)
    {
        $user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
            'active' => 1
        ]);

        $user->user_data()->create($data['user_data']);
        $user->assignRole(1);

//        try {
//            Mail::send('emails.register', ['user' => $user], function ($message) use ($user) {
//                $message->subject('Gracias por registrarte');
//                $message->to($user->email);
//                foreach(toArray(options('contact_email')) as $line) {
//                    if(filter_var($line, FILTER_VALIDATE_EMAIL)) $message->cc($line);
//                }
//                $message->from(env('MAIL_FROM_ADDRESS'), options('title'));
//            });
//
//        } catch(\Exception $e) {
//        }

        return $user;
    }
}
