<?php

namespace App\Http\Requests;

use App\Models\User;
use App\Support\Backoffice\Sanitizer\Traits\SanitizesInput;
use Illuminate\Foundation\Http\FormRequest;

class EmailVerification extends FormRequest
{
    use SanitizesInput;

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        $id = $this->route('id');
        $hash = $this->route('hash');

        if(empty($id) || empty($hash)) {
            return false;
        }

        $user = User::find($id);

        if(empty($user)) {
            return false;
        }

        if (! hash_equals(sha1($user->getEmailForVerification()), (string) $hash)) {
            return false;
        }

        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            //
        ];
    }

    /**
     * Configure the validator instance.
     *
     * @param  \Illuminate\Validation\Validator  $validator
     * @return void
     */
    public function withValidator($validator)
    {
        return $validator;
    }
}
