<?php

namespace Wave\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Mail\ForgetPasswordOtp;
use App\Models\User;
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Mail;
use Illuminate\Http\Request;

class ForgotPasswordController 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.
    |
    */

    use SendsPasswordResetEmails;

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

    /**
     * Display the form to request a password reset link.
     *
     * @return \Illuminate\Http\Response
     */
    public function showLinkRequestForm()
    {
        return view('theme::auth.passwords.email');
    }

    public function showRequestForm()
    {
        // return view('theme::auth.passwords.email');
        return view('theme::auth.passwords.user-reset');
    }
    
    public function sendOtp(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'email' => 'required|string|email|max:255',
        ]);
        if ($validator->fails()) {
            return response()->json([
                'success' => false,
                'errors' => $validator->errors()
            ], 422);
        }

        $user = User::where('email', $request->email)->first();
        if (!$user) {
            return response()->json([
                'success' => false,
                'message' => "Account doesn't exists!",
            ]);
        }

        $otp = null;
        while (true) {
            $otp = random_int(100000, 999999);
            $otpExists = User::where('forgot_password_otp', $otp)->first();
            if (!$otpExists) {
                break;
            }
        }

        $user->forgot_password_otp = $otp;
        $user->save();

        Mail::to($request->email)->send(new ForgetPasswordOtp($otp));

        return response()->json([
            'success' => true,
        ]);
    }
    
    public function submitOtp(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'otp' => 'required|string|max:6',
        ]);
        if ($validator->fails()) {
            return response()->json([
                'success' => false,
                'errors' => $validator->errors()
            ], 422);
        }

        $user = User::where('forgot_password_otp', $request->otp)->first();
        if (!$user) {
            return response()->json([
                'success' => false,
                'message' => 'Wrong OTP',
            ]);
        }

        $user->forgot_password_otp = null;
        $user->save();

        return response()->json([
            'success' => true,
            'user_id' => $user->id,
        ]);
    }
    
    public function resetPassword(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'user_id' => 'required',
            'password' => 'required|string|min:6|confirmed',
        ], [
            'password.confirmed' => 'Wrong Password',
        ]);
        if ($validator->fails()) {
            return response()->json([
                'success' => false,
                'errors' => $validator->errors()
            ], 422);
        }

        $user = User::find($request->user_id);
        if (!$user) {
            return response()->json([
                'success' => false,
                'message' => "User doesn't exists!",
            ]);
        }

        $user->password = bcrypt($request->password);
        $user->save();

        return response()->json([
            'success' => true,
            'message' => 'Password has successfully reset'
        ]);
    }
}
