<?php

namespace Wave\Http\Controllers\API;

use App\Http\Controllers\Controller;
use App\Mail\ForgetPasswordOtp;
use App\Models\Company;
use App\Models\Role;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
use Tymon\JWTAuth\Facades\JWTAuth;
use Wave\ApiKey;
use Wave\Notifications\VerifyEmail;

class ForgotPasswordController extends Controller
{
    /**
     * Create a new AuthController instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth:api');
    }

    public function sendOtp(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'email' => 'required|string|email|max:255',
        ]);
        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 422);
        }

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

        $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(['message' => 'OTP has successfully sent!']);
    }
    
    public function submitOtp(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'otp' => 'required|string|max:6',
        ]);
        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 422);
        }

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

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

        return response()->json([
            'message' => 'success',
            '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(['error' => $validator->errors()], 422);
        }

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

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

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