<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\Models\User;
use App\Models\CityRange;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

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'],
            'email'        => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'phone_number' => ['required', 'string', 'max:20'],
            'password'     => ['required', 'string', 'min:6', 'confirmed'],
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\Models\User
     */
    protected function create(array $data)
    {
        $type = request('type');
        if($type == 'buying'){
            return User::create([
                'name'         => $data['name'],
                'email'        => $data['email'],
                'phone_number' => $data['phone_number'],
                'password'     => Hash::make($data['password']),
                'user_type'    => '3',
                'user_name'    => 'Anonymous_'.rand(100, 1000)
            ]);
        }
        if($type == 'realtor'){
            
            $language = NULL;
            if($data['language']){
                $language = explode(', ',$data['language']);
            }
            
            $user = User::create([
                        'name'                     => $data['name'],
                        'email'                    => $data['email'],
                        'phone_number'             => $data['phone_number'],
                        'password'                 => Hash::make($data['password']),
                        'user_type'                => '2',
                        'user_name'                => 'Realtor_'.rand(100, 1000),
                        'brokerage'                => $data['brokerage'],
                        'realtor_year'             => $data['years_of_realtor'],
                        'real_estate_association'  => $data['real_estate_association'],
                        'years_with_brokerage'     => $data['years_with_brokerage'],
                        'address'                  => $data['address'],
                        'awards'                   => $data['awards'],
                        'realtor'                  => $data['realtor_number'],
                        'language'                 => $language ? json_encode($language) : NULL,
                        'website'                  => $data['website'],
                        'bio'                      => $data['bio'],
                        'sports_played'            => $data['sports_played'],
                        'interest_and_hobby'       => $data['interests_and_hobbies'],
                        'other_language'           => $language && in_array('Other',$language) ? $data['other_language'] : NULL,
                    ]);
            
            $city      = $data['city'];
            $cityRange = $data['city_range'];
            
            foreach($cityRange as $key => $value){
                $cityRangeData = new CityRange();
                $cityRangeData->user_id    = $user->id;
                $cityRangeData->city_name  = $city[$key];
                $cityRangeData->city_range = $value;
                $cityRangeData->save();
            }
            
            return $user;
        }
    }
}
