<?php

namespace App\Http\Controllers\TeacherAuth;

use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Auth;
use App\Mail\SignUp;
use App\User;
use Hash;
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 login / registration.
     *
     * @var string
     */
    //protected $redirectTo = '/login-v2';

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


    protected function validator(array $data)
    {
        return Validator::make($data, [
            'firstname' => ['required', 'string', 'max:255'],
            'lastname' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
            //'phonenumber' => ['required', 'string', 'max:255'],
            'universityname' => ['required', 'string', 'max:255'],
            'department' => ['required', 'string', 'max:255'],
            'designation' => ['required', 'string', 'max:255'],
            'universitywebsite' => ['required', 'string', 'max:255'],
        ]);
    }

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

        $data = User::create([
            'first_name' => $req->firstname,
            'last_name' => $req->lastname,
            'password' => Hash::make($req->password),
            'email' => $req->email,
           // 'phone_number' => $req->phonenumber,
            'university_name' => $req->universityname,
            'department' => $req->department,
            'designation' => $req->designation,
            'uni_website' => $req->universitywebsite,
            'user_type' => $req->user_type,
            'ref_no' => name_initials($req->firstname,$req->lastname).''.teacherRefNoGenerate(),
        ]);
       Mail::to('test@mail.com')->send(new SignUp($data->id));
        return redirect()->to("/login-v2");

    }

    /**
     * Show the application registration form.
     *
     * @return \Illuminate\Http\Response
     */
    public function showRegistrationForm()
    {

        return view('register-v2');
    }

    /**
     * Get the guard to be used during registration.
     *
     * @return \Illuminate\Contracts\Auth\StatefulGuard
     */
    protected function guard()
    {
        return Auth::guard('teacher');
    }
}
