<?php

use App\Models;
use App\Models\IamPrincipal;
use Illuminate\Support\Facades\Session;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Tymon\JWTAuth\Facades\JWTAuth;

/**
 * Created By : sayli raut
 * Created at : 24 Jan 2024
 * Use : Json response with success message for API
 */
if (!function_exists('jsonResponseWithSuccessMessageApi')) {
    function jsonResponseWithSuccessMessageApi($message, $data = [], $statusCode = 200)
    {
        // Set the HTTP status code
        http_response_code($statusCode);

        // Prepare the response array
        $response = [
            'status' => 'success',
            'status_code' => $statusCode,
            'message' => $message,
            'data' => $data,
        ];
        return response()->json($response, $statusCode);

        // Stop further execution (optional)
        exit();
    }
}

/**
 * Created By : sayli raut
 * Created at : 24 jan 2024
 * Use : Json response with error message for API
 */
if (!function_exists('jsonResponseWithErrorMessageApi')) {
    function jsonResponseWithErrorMessageApi($errorMessage, $statusCode = 500)
    {
        // Set the HTTP status code
        http_response_code($statusCode);

        // Prepare the response array
        $response = [
            'status' => 'error',
            'status_code' => $statusCode,
            'message' => $errorMessage,
        ];
        return response()->json($response, $statusCode);

        // Stop further execution (optional)
        exit();
    }
}

/**
 * Created by : sayli raut
 * Created at : 24 Jan 2024
 * Use : To return error json response for admin
 */
if (!function_exists('jsonResponseWithErrorMessage')) {
    function jsonResponseWithErrorMessage($errorMessage)
    {
        $response = [
            'status' => 'error',
            'message' => $errorMessage,
        ];
        return response()->json($response);

        // Stop further execution (optional)
        exit();
    }
}

/**
 * Created by : sayli raut
 * Created at : 24 Jan 2024
 * Use : To return success json response for admin
 */
if (!function_exists('jsonResponseWithSuccessMessage')) {
    function jsonResponseWithSuccessMessage($message, $data = [])
    {
        $statusCode = 200;
        // Prepare the response array
        $response = [
            'status' => 'success',
            'status_code' => $statusCode,
            'message' => $message,
            'data' => $data,
        ];
        return response()->json($response, $statusCode);

        // Stop further execution (optional)
        exit();
    }
}

/**
 *   Created by : Pradyumn Dwivedi
 *   Created On : 11-May-2022
 *   Uses: This function will be used to full search data in api.
 */
if (!function_exists('fullSearchQuery')) {
    function fullSearchQuery($query, $word, $columns)
    {
        $orwords = explode('|', $columns);
        $query = $query->where(function ($query) use ($word, $orwords) {
            foreach ($orwords as $key) {
                $query->orWhere($key, 'like', '%' . $word . '%');
            }
        });
        return $query;
    }
}

/**
 * Created by : sayli raut
 * Created at : 24 Feb 2024
 * Use : To check and validate to customer token
 */
if (!function_exists('readHeaderToken')) {
    function readHeaderToken()
    {
        $tokenData = Session::get('vendorToken');
        $token = JWTAuth::setToken($tokenData)->getPayload();
        // dd("tokendata",$tokenData,$token['sub'],$token['iat']);
        //convert iat to readable format
        $iat = date('Y-m-d H:i:s', $token['iat']);

        // check token issued time for single device login
        // ['last_login_datetime',  $iat]

        $check_iat = IamPrincipal::where([['id', $token['sub']],])->first();
        // dd($check_iat);
        if ($check_iat) {
            return $token;
        } else {
            return false;
        }
    }
}

/**
 * Created by : sayli raut
 * Created at : 24 jan 2024
 * Use : To check and validate login restaurant user token
 */
if (!function_exists('readRestHeaderToken')) {
    function readRestHeaderToken()
    {
        $tokenData = Session::get('RestToken');
        $token = JWTAuth::setToken($tokenData)->getPayload();

        //convert iat to readable format
        $iat = date('Y-m-d H:i:s', $token['iat']);

        // check token issued time for single device login
        $check_iat = IamPrincipal::where([['id', $token['sub']], ['is_active', '1']])->first();
        if ($check_iat) {
            return $token;
        } else {
            return false;
        }
    }

    if (!function_exists('generateOTP')) {

    function generateOTP()
    {
        // Define the length of the OTP
        $otpLength = 4;

        // Generate a random OTP with $otpLength digits
        $otp = '';
        for ($i = 0; $i < $otpLength; $i++) {
            $otp .= rand(0, 9);
        }
        return $otp;
    }
}
}
