<?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 App\Models\Department;
use App\Models\University;
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)
    {
        $user_check_data =User::where('email',$req->email)->where('user_type',2)->where('sign_up',1)->doesntExist();

        if($user_check_data){ 
            $user_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->university,
                'department' => $req->department,
                'others_department' => $req->others_department,
                'designation' => $req->designation,
                'uni_website' => $req->universitywebsite,
                'user_type' => 2,
                'ref_no' => name_initials($req->firstname,$req->lastname).''.teacherRefNoGenerate(),
                'status' =>1,
                'sign_up' => 1, // 1=signup
                'registered_date' =>now(), 
            ]);

        }else{
            return redirect()->to("/login-v2")->with('success','You are already registered. Please login here.');
        }
        
        $data['name'] = $user_data->first_name .' '.$user_data->last_name;
        $data['receive_email']=$user_data->email;
        $user_mail = $user_data->email;
        $from = 'asraful@revinr.com';

        Mail::send('email.register_teacher_email',$data, function ($message) use ($user_mail,$from) {
            $message->from($from);
            $message->to($user_mail)->subject('We’re excited to have you as a member of our community');
        });

        return redirect()->to("/login-v2")->with('success','Your registration successfully saved.');

    }

    /**
     * Show the application registration form.
     *
     * @return \Illuminate\Http\Response
     */
    public function showRegistrationForm()
    {
        $departments =Department::where('status',1)->orderBy('name','ASC')->get();
        $universities =University::orderBy('name','ASC')->get();
        return view('register-v2',compact('departments','universities'));
    }

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