<?php

namespace modules\lead_manager\core;

defined('BASEPATH') or exit('No direct script access allowed');
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../third_party/node.php';

use \Firebase\JWT\JWT as LMJWT;
use \Firebase\JWT\Key as LMKEY;
use WpOrg\Requests\Requests as Lm_Requests;

class Leadmanagerinit
{
    public static function check_url($module_name)
    {
        $CI       = &get_instance();
        $verified = false;

        if (!option_exists($module_name . '_verification_id') || !option_exists($module_name . '_verified') || 1 != get_option($module_name . '_verified')) {
            $verified = false;
        }
        $verification_id =  get_option($module_name . '_verification_id');
        $id_data         = explode('|', $verification_id);
        if (4 != count($id_data)) {
            $verified = false;
        }

        if (file_exists(__DIR__ . '/../config/token.php') && 4 == count($id_data)) {
            $verified = false;
            $token    = file_get_contents(__DIR__ . '/../config/token.php');
            if (empty($token)) {
                $verified = false;
            }
            $CI->load->config($module_name . '/conf');
            try {
                $data = LMJWT::decode($token, new LMKEY($id_data[3], 'HS512'));
                if (!empty($data)) {
                    if ($CI->config->item('product_item_id') == $data->item_id && $data->item_id == $id_data[0] && $data->buyer == $id_data[2] && $data->purchase_code == $id_data[3]) {
                        $verified = true;
                    }
                }
            } catch (\Firebase\JWT\SignatureInvalidException $e) {
                $verified = false;
            }

            $last_verification = get_option($module_name . '_last_verification');
            $seconds           = $data->id ?? 0;
            if (empty($seconds)) {
                $verified = false;
            }
            if ('' == $last_verification || (time() > ($last_verification + $seconds))) {
                $verified = false;
                try {
                    $headers  = ['Accept' => 'application/json', 'Authorization' => $token];
                    $request  = Lm_Requests::post(LM_VAL_PROD_POINT, $headers, json_encode(['verification_id' => $verification_id, 'item_id' => $CI->config->item('product_item_id')]));
                    if ((500 <= $request->status_code) && ($request->status_code <= 599) || 404 == $request->status_code) {
                        $verified = true;
                    } else {
                        $result   = json_decode($request->body);
                        if (!empty($result->valid)) {
                            $verified = true;
                        }
                    }
                } catch (Exception $e) {
                    $verified = true;
                }
                update_option($module_name . '_last_verification', time());
            }
        }

        if (!file_exists(__DIR__ . '/../config/token.php') && !$verified) {
            $last_verification = get_option($module_name . '_last_verification');
            if (((int)$last_verification + (168 * (3000 + 600))) > time()) {
                $verified = true;
            }
        }
        if (!$verified) {
            $CI->app_modules->deactivate($module_name);
        }

        return $verified;
    }

    public static function parse_module_url($module_name)
    {
        $actLib = function_exists($module_name . "_actLib");
        $verify_module = function_exists($module_name . "_sidecheck");
        $deregister = function_exists($module_name . "_deregister");

        if (!$actLib || !$verify_module || !$deregister) {
            $CI       = &get_instance();
            $CI->app_modules->deactivate($module_name);
        }
    }
}
