<?php

namespace App\Http\Controllers;

use App\Models\User;
use App\Models\Website;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Session;

class OtpController extends Controller
{
    public function SearchNumber(Request $request)
    {
        $request->validate([
            'value'    =>  'required|min:10'
        ]);
        $mobile = $request->value;
        $otp = rand(1000, 10000);
        $users = User::where(['mobile'   =>  $mobile])->get();
        if (count($users) == 0) {
            Log::info('otp ' . $otp);
            Session::put("signup_mobile", $mobile);
            Session::put("signup_otp", $otp);
            $website_settings = Website::first();
            $message = $otp;
            if (env('FAST_2_SMS_AUTH') && env('FAST_2_SMS_AUTH') != "") {
                $sms_api_token = env('FAST_2_SMS_AUTH');
            } else {
                $sms_api_token = $website_settings->sms_api;
            }
            $fields = [
                "route" => "otp",
                // "variables_values" => $message,
                "variables_values" => $message,
                "language" => "english",
                "flash" => 0,
                "numbers" => $mobile,
            ];
            try {
                $http_request = Http::withHeaders([
                    "Content-Type" => "application/json",
                    "Authorization" => $sms_api_token,
                ])->post("https://www.fast2sms.com/dev/bulkV2", $fields);

                $response = $http_request->json();
                Log::info('response otp' . print_r($response, true));


                if (!$http_request->ok()) {
                    Log::info('response ' . print_r($response, true));
                    return response()->json(['status'   =>  'error', 'message' => "Something went wrong!"]);
                } elseif (!$response['return']) {
                    return response()->json(['status'   =>  'invalid', 'message' => $response['message'], $otp]);
                }
            } catch (\Throwable $th) {
                return response()->json(['status'   =>  'success', 'message' => 'OTP Sent', 'otp' => $otp]);
            }
            return response()->json(['status'   =>  'success', 'message' => $response, 'otp' => $otp]);
        } else {
            return response()->json(['status'   => 'error', 'message' => "User already created!"]);
        }
    }

    public function CheckOtp(Request $request)
    {
        $request->validate([
            'value' =>  'required'
        ]);
        $value = $request->value;

        if (otp_verification($value)) {
            return response()->json(['status'   =>  'success']);
        } else {
            return response()->json(['status'   =>  'error']);
        }
    }

    public function ConfirmNumber(Request $request)
    {
        $request->validate([
            'value'    =>  'required|min:10'
        ]);
        $mobile = $request->value;
        $otp = rand(1000, 10000);
        $users = User::where(['mobile'   =>  $mobile])->get();
        if (count($users) > 0) {
            $website_settings = Website::first();
            Session::put("signup_mobile", $mobile);
            Session::put("signup_otp", $otp);
            $message = $otp;
            if (env('FAST_2_SMS_AUTH') && env('FAST_2_SMS_AUTH') != "") {
                $sms_api_token = env('FAST_2_SMS_AUTH');
            } else {
                $sms_api_token = $website_settings->sms_api;
            }
            $fields = [
                "route" => "otp",
                "variables_values" => $message,
                "language" => "english",
                "flash" => 0,
                "numbers" => $mobile,
            ];
            $http_request = Http::withHeaders([
                "Content-Type" => "application/json",
                "Authorization" => $sms_api_token,
            ])->post("https://www.fast2sms.com/dev/bulkV2", $fields);

            $response = $http_request->json();
            Log::info('response otp' . print_r($response, true));

            if (!$http_request->ok()) {
                return response()->json(['status'   =>  'error', 'message'    =>  "Something went wrong!"]);
            } elseif (!$response['return']) {
                return response()->json(['status'   =>  'invalid', 'message' => $response['message']]);
            }
            return response()->json(['status'   =>  'success', 'message'    =>  $response, 'otp'    =>  $otp]);
        } else {
            return response()->json(['status'   =>  'invalid']);
        }
    }

    public function ResetPasswordOtp()
    {
        $mobile = Auth::user()->mobile;
        $otp = rand(1000, 10000);
        Log::info('reset otp ' . $otp);
        $users = User::where(['mobile'   =>  $mobile])->get();
        if (count($users) > 0) {
            Session::put("reset_otp", $otp);
            $website_settings = Website::first();
            $message = $otp;
            if (env('FAST_2_SMS_AUTH') && env('FAST_2_SMS_AUTH') != "") {
                $sms_api_token = env('FAST_2_SMS_AUTH');
            } else {
                $sms_api_token = $website_settings->sms_api;
            }
            $fields = [
                "route" => "otp",
                "variables_values" => $message,
                "language" => "english",
                "flash" => 0,
                "numbers" => $mobile,
            ];
            $http_request = Http::withHeaders([
                "Content-Type" => "application/json",
                "Authorization" => $sms_api_token,
            ])->post("https://www.fast2sms.com/dev/bulkV2", $fields);

            $response = $http_request->json();
            Log::info('response otp' . print_r($response, true));

            if (!$http_request->ok()) {
                return response()->json(['status' => 'error', 'message' => 'Something went wrong!']);
            } elseif (!$response['return']) {
                return response()->json(['status'   =>  'invalid', 'message' => $response['message']]);
            }
            return response()->json(['status' => 'success', 'message' => $response, 'otp' => $otp]);
        } else {
            return response()->json(['status'   =>  'invalid', 'message'    =>  "Something went wrong!"]);
        }
    }

    public function CheckResetPasswordOtp(Request $request)
    {
        $request->validate([
            'value' =>  'required'
        ]);
        $value = $request->value;

        if (Session::has('reset_otp') && Session::get('reset_otp') == $value) {
            return response()->json(['status'   =>  'success']);
        } else {
            return response()->json(['status'   =>  'error']);
        }
    }
}
