<?php

namespace App\Http\Controllers\Auth;

use App\City;
use App\Country;
use App\Http\Controllers\Controller;
use App\Month;
use App\Providers\RouteServiceProvider;
use App\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller.blade.php handles the registration of new users as well as their
    | validation and creation. By default this controller.blade.php 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.blade.php 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'],
            'surname' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'username' => ['required', 'string', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
            'day' => ['required', 'integer'],
            'month' => ['required', 'string'],
            'year' => ['required', 'integer'],
            'country' => ['required', 'integer'],
            'city' => ['required', 'integer'],
            'skill' => ['required', 'string'],
            'about' => ['required', 'string'],
        ]);
    }

    public function showRegistrationForm()
    {
        return view('auth.register', [
            "months" => Month::all(),
            "countries" => Country::all(),
            "cities" => City::where("country_id", 1)->get(),
            "year" => [
                "min" => date('Y') - 90,
                "max" => date('Y') - 14
            ]
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'surname' => $data['surname'],
            'username' => $data['username'],
            'dob' => $data['day'] . " " . $data['month'] . " " . $data['year'],
            'country_id' => $data["city"],
            'city_id' => $data["country"],
            'email' => $data['email'],
            'skill' => $data['skill'],
            'about' => $data['about'],
            'password' => Hash::make($data['password']),
        ]);
    }
}
