<?php
namespace App\Http\Controllers\Auth;

use App\Models\User;
use App\Http\Controllers\Controller;

class ForgotPasswordPhoneController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Password Reset Controller
    |--------------------------------------------------------------------------
    |
    | This controller is responsible for handling password reset emails and
    | includes a trait which assists in sending these notifications from
    | your application to your users. Feel free to explore this trait.
    |
    */

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  string  $data
     * @return \App\User
     */
    protected function getUser(string $phone_number)
    {
        return User::where('phone_number', $phone_number)->first();
    }

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

        if (is_null($user = $this->getUser($request->phone_number))) {
            return $this->sendResetCodeFailedResponse($request);
        }
        event(new ResetedPassword($user));
        return $this->sendResetCodeResponse($request, $user);
    }

    /**
     * Resend a reset code to the given user.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function resendResetCodePhoneNumber(Request $request)
    {
        $this->validatePhoneNumber($request);

        if (is_null($user = $this->getUser($request->phone_number))) {
            return $this->sendResetCodeFailedResponse($request);
        }
        event(new ResetedPassword($user));
        return $this->resendResetCodeResponse($request, $user);
    }

    /**
     * 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|regex:/^([0-9\s\-\+\(\)]*)$/|min:10']);
    }

    /**
     * Get the response for a successful password reset code.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  string  $response
     */
    protected function sendResetCodeResponse(Request $request, $user)
    {
        return response()->json(['status'=>'ok', 'message' => __('passwords.send_code_succesfully'), 'id' => $user->id],200);
    }

    /**
     * Get the response for a successful password reset code.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  string  $response
     */
    protected function resendResetCodeResponse(Request $request, $user)
    {
        return response()->json(['status'=>'ok', 'message' => __('passwords.resend_code_succesfully'), 'id' => $user->id],200);
    }

    /**
     * Get the response for a failed password reset code.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  string  $response
     */
    protected function sendResetCodeFailedResponse(Request $request)
    {
        return response()->json(['status'=>'failed', 'message' => __('passwords.phone_not_found')],422);
    }

}