<?php

namespace App\Http\Controllers\Auth;

use Exception;
use Illuminate\Http\Request;
use Illuminate\Auth\Events\Verified;
use Illuminate\Auth\Events\Registered;
use Illuminate\Database\QueryException;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Log;

class VerificationPhoneController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Phone Verification Controller
    |--------------------------------------------------------------------------
    |
    | This controller is responsible for handling phone verification for any
    | user that recently registered with the application. Phone numbers may also
    | be re-sent if the user didn't receive the original sms message.
    |
    */

    /**
     * Send a code to verify code.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
     */
    public function send(Request $request)
    {
        $this->validatePhoneNumber($request);

        try {
            $this->updateUser($request);
        } catch (QueryException $e) {
            Log::error($e);
            return response()->json(['status'=>'failed', 'message' => __('verification.already_phone_exist')],422);
        }

        event(new Registered($request->user()));
        return response()->json(['status'=>'ok', 'message' => __('verification.send_code_succesfully')],200);
    }

    /**
     * Mark the authenticated user's number phone as verified.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function verify(Request $request)
    {
        if (strval($request->user()->verification_code) !== strval($request->verification_code)) {
            return response()->json(['status'=>'failed', 'message' => __('verification.code_failed')],422);
        }

        if ($request->user()->hasVerifiedPhone()) {
            return response()->json(['status'=>'ok','message' => __('verification.success')],200);
        }

        if ($request->user()->markPhoneAsVerified()) {
            event(new Verified($request->user()));
        }

        return response()->json(['status'=>'ok','message' => __('verification.success')],200);
    }

    /**
     * Resend the phone verification notification.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function resend(Request $request)
    {
        $this->validatePhoneNumber($request);

        if ($request->user()->hasVerifiedPhone()) {
           return response()->json(['status'=>'failed', 'message' => __('verification.already_verified')],422);
        }

        $request->user()->sendPhoneVerificationNotification();

        return response()->json(['status'=>'ok','message' => __('passwords.resend_code_succesfully')],200);
    }

    /**
     * Validate the phone_number for the given request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return void
     */
    protected function validatePhoneNumber(Request $request)
    {
        $request->validate(['phone_number' => 'required|unique:users,id,'.$request->user()->id.'|regex:/^([0-9\s\-\+\(\)]*)$/|min:10']);
    }

    /**
     * Update current user.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return User $user
     */
    protected function updateUser(Request $request)
    {
        $user = $request->user();
        $user->phone_number = $request->phone_number;
        $user->save();
    }
}
