<?php
function find_wordpress_root($start_path) {
    while (!file_exists($start_path . '/wp-load.php')) {
        $start_path = dirname($start_path);
        if ($start_path === '/') {
            return false;
        }
    }
    return $start_path;
}
$wordpress_root = find_wordpress_root(__DIR__);
require_once $wordpress_root. '/wp-load.php';
global $wpdb;
$checkInstall=$wpdb->get_row("select * from `".$wpdb->prefix ."hd_webapp_installed` where webapp_id='".$_REQUEST["app_wp_id"]."' and application_id='".$_REQUEST["application_id"]."'" );
if(empty($checkInstall)) {
    $args=(isset($args))?$args:[];
    $defaults = array(
        'webapp_id' =>$_REQUEST["app_wp_id"],
        'application_id' => $_REQUEST["application_id"]??'',
        'phone_type' => $_REQUEST["phone_type"]??'',
        'phone_model' => $_REQUEST["phone_model"]??'',
    );
    $args = wp_parse_args($args, $defaults);
    $table_name = $wpdb->prefix . 'hd_webapp_installed';
    if ($wpdb->insert($table_name, $args)) {
        $response = ["status" => true];
        echo json_encode($response);
    }
    else{
        $response = [
            "status" => false,
            "error" => $wpdb->last_error, // This gives the MySQL error message
            "query" => $wpdb->last_query, // Optional: shows the last executed query
        ];
        echo json_encode($response);
    }
//    $setInsert=$wpdb->get_row("INSERT INTO `".$wpdb->prefix ."hd_webapp_installed` ( `webapp_id`, `application_id`, `phone_type`, `phone_model`) VALUES ( '".$_REQUEST["app_wp_id"]."', '".$_REQUEST["application_id"]."', '44', '44')" );
//    $response = ["status" => true];
//    echo json_encode($response);
}else{
    $response = ["status" => false];
    echo json_encode($response);
}
?>