<?php

if (! defined('ABSPATH')) {
    exit;
} class PufferGo_Telemetry
{
    public static function register_hooks()
    {
        add_action('admin_enqueue_scripts', array(__CLASS__, 'enqueue_global_telemetry_context'), 1);
        add_action('enqueue_block_editor_assets', array(__CLASS__, 'enqueue_global_telemetry_context'), 1);
        add_action('puffergo_send_error_event_async', array(__CLASS__, 'handle_async_error_event'));
    }

    public static function enqueue_global_telemetry_context()
    {
        if (! is_user_logged_in() || ! current_user_can('edit_posts')) {
            return;
        } static $done = false;

        if ($done) {
            return;
        } $done = true;
        $handle = 'puffergo-telemetry-context';
        $version = defined('PUFFERGO_CORE_VERSION') ? PUFFERGO_CORE_VERSION : '1.0.0';
        wp_register_script($handle, false, array(), $version, false);
        wp_enqueue_script($handle);
        $payload = array('baseUrl' => rtrim(self::get_api_base_url(), '/'), 'env' => self::get_env_payload(), );
        wp_add_inline_script($handle, 'window.puffergoTelemetry=' . wp_json_encode($payload, JSON_UNESCAPED_UNICODE) . ';', 'after');
    }

    public static function handle_async_error_event(array $payload)
    {
        self::send_event($payload);
    }

    public static function get_api_base_url()
    {
        return defined('PUFFERGO_AI_PAGE_BUILDER_BASE_URL') ? PUFFERGO_AI_PAGE_BUILDER_BASE_URL : 'https://api.puffergo.com';
    }

    public static function get_env_payload()
    {
        return array('domain' => home_url(), 'phpVersion' => PHP_VERSION, 'wpVersion' => get_bloginfo('version'), 'pufferGoVersion' => defined('PUFFERGO_CORE_VERSION') ? PUFFERGO_CORE_VERSION : '', );
    }

    public static function send_download_event()
    {
        $payload = array_merge(array('eventType' => 'download'), self::get_env_payload());
        self::send_event($payload);
    }

    public static function send_activate_event()
    {
        $payload = array_merge(array('eventType' => 'activate'), self::get_env_payload());
        self::send_event($payload);
    }

    public static function send_deactivate_event($reason = '', $reason_detail = '')
    {
        $payload = array_merge(array('eventType' => 'deactivate', 'uninstallReason' => is_string($reason) ? $reason : '', 'uninstallReasonDetail' => is_string($reason_detail) ? $reason_detail : '', ), self::get_env_payload());
        self::send_event($payload);
    }

    public static function send_uninstall_event($reason = '', $reason_detail = '')
    {
        $payload = array_merge(array('eventType' => 'uninstall', 'uninstallReason' => is_string($reason) ? $reason : '', 'uninstallReasonDetail' => is_string($reason_detail) ? $reason_detail : '', ), self::get_env_payload());
        self::send_event($payload);
    }

    private static function send_event($payload)
    {
        $url = self::get_api_base_url() . '/api/telemetry/event';
        $body = wp_json_encode($payload);

        if ($body === false) {
            return;
        } $args = array('headers' => array('Content-Type' => 'application/json; charset=utf-8', ), 'body' => $body, 'timeout' => 5, );
        $response = wp_remote_post($url, $args);

        if (is_wp_error($response)) {
            $response = wp_remote_post($url, $args);
        }

if (is_wp_error($response)) {
            return;
        } $code = wp_remote_retrieve_response_code($response);
        $code = is_numeric($code) ? (int) $code : 0;

        if ($code >= 500 && $code < 600) {
            wp_remote_post($url, $args);
        }
    }

    public static function send_error_event(array $detail)
    {
        if (empty($detail)) {
            return;
        } $json = wp_json_encode($detail, JSON_UNESCAPED_UNICODE);

        if (false === $json) {
            return;
        } $payload = array_merge(array('eventType' => 'error', 'content' => $json, ), self::get_env_payload());
        wp_schedule_single_event(time(), 'puffergo_send_error_event_async', array($payload));
    }

    public static function get_feedback_url()
    {
        $base = defined('PUFFERGO_FEEDBACK_BASE_URL') ? PUFFERGO_FEEDBACK_BASE_URL : (defined('PUFFERGO_BASE_URL') ? PUFFERGO_BASE_URL : 'https://puffergo.com');
        $base = rtrim($base, '/');
        $args = array('domain' => home_url(), 'php_version' => PHP_VERSION, 'wp_version' => get_bloginfo('version'), 'puffergo_version' => defined('PUFFERGO_CORE_VERSION') ? PUFFERGO_CORE_VERSION : '', );

        return $base . '/feedback?' . http_build_query($args);
    }
}
