<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\Models\User;
use App\Models\HCountry;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use App\Rules\NameRule;
use App\Rules\PasswordRule;
use Auth;
use Illuminate\Support\Facades\Mail;
use Illuminate\Auth\Events\Registered;

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 = RouteServiceProvider::HOME;

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

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        $validate = [            
            'name'          => ['required', 'string', 'max:255', new NameRule],
            'email'         => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password'      => ['required', 'string', 'min:8', 'max:32', 'confirmed', new PasswordRule],
            'company'       => ['required', 'string', 'max:255'],
            'phone'         => ['required', 'string', 'max:20'],
            'country'       => ['required', 'string'],
        ];

        if($this->request->hasFile('avatar')) {
            $validate['avatar'] =  ['mimes:jpeg,jpg,png,gif', 'max:2048'];
        }
        
        return Validator::make($data, $validate);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\Models\User
     */
    protected function create(array $data)
    {
        if($this->request->hasFile('avatar')) {
            $avatarPath = $this->request->file('avatar')->store('avatar', 'public');
        } else {
            $avatarPath = '';
        }
        $confirmation_code = time().uniqid(true);
        
        $user = User::create([
            'name'              => $data['name'],
            'email'             => $data['email'],
            'password'          => Hash::make($data['password']),
            'phone'             => $data['phone'],
            'company'           => $data['company'],
            'avatar'            => $avatarPath,
            'country_code'      => $data['country'],
            'confirmation_code' => $confirmation_code,
            'status'            => 0,
        ]);

        
        return $user;
    }
    
    public function register(Request $request)
    {
        $this->validator($request->all())->validate();
        $user = $this->create($request->all());
        event(new Registered($user));
        if ($response = $this->registered($request, $user)) {
            return $response;
        }
        return redirect()->route('verification.notice')->with('confirmation_code', $user['confirmation_code']);
    }

    public function showRegistrationForm()
    {
        $countries = HCountry::pluck('name', 'code'); 
        return view('auth.register', compact('countries'));
    }
}


