<?php

// Exit if accessed directly.
if (!defined('ABSPATH')) {
    exit;
}


// Register the action hook
add_action('admin_init', 'integrazo_wz_errorlog_resync_action');

function integrazo_wz_errorlog_resync_action()
{
    if (
        isset($_GET['action'], $_GET['page'], $_GET['log_id'], $_GET['_wpnonce']) &&
        $_GET['action'] === 'integrazo_wz_resync' &&
        $_GET['page'] === 'integrazo-integration-for-zoho-apps'
    ) {
        $log_id = absint($_GET['log_id']);

        // Verify the nonce
        if (
            ! isset($_GET['_wpnonce']) ||
            ! wp_verify_nonce(
                sanitize_text_field(wp_unslash($_GET['_wpnonce'])),
                'resync_error_log_' . $log_id
            )
        ) {
            wp_die(esc_html__('Security check failed.', 'integrazo-integration-for-zoho-apps'));
        }
        if (! current_user_can('manage_options')) {
            wp_die(esc_html__('Insufficient permissions.', 'integrazo-integration-for-zoho-apps'));
        }

        // Log Resync is a Pro-only feature — the free plugin only checks for a
        // valid Pro session and delegates the actual resync to the Pro plugin.
        if (! integrazo_wz_session()) {
            set_transient(
                'integrazo_wz_fail',
                __('Log Resync is a Pro feature. <a href="https://integrazo.com/products/integration-for-zoho-apps" target="_blank">Upgrade to Pro</a> to use this feature.', 'integrazo-integration-for-zoho-apps'),
                30
            );
            wp_safe_redirect(remove_query_arg(['log_id', '_wpnonce', 'action']));
            exit;
        }

        integrazo_wz_pro_do_single_resync($log_id);

        // Redirect to remove sensitive query args
        wp_safe_redirect(remove_query_arg(['log_id', '_wpnonce', 'action']));
        exit;
    }
}

function integrazo_wz_bulk_resync(array $log_ids)
{
    // Log Resync is a Pro-only feature — the free plugin only checks for a
    // valid Pro session and delegates the actual resync to the Pro plugin.
    if (! integrazo_wz_session()) {
        set_transient(
            'integrazo_wz_fail',
            __('Log Resync is a Pro feature. <a href="https://integrazo.com/products/integration-for-zoho-apps" target="_blank">Upgrade to Pro</a> to use this feature.', 'integrazo-integration-for-zoho-apps'),
            30
        );
        return;
    }

    integrazo_wz_pro_bulk_resync($log_ids);
}



function INTEGRAZO_WZ_Error_Log_show_action()
{
    // Check the nonce for security
    if (
        !isset($_POST['security']) ||
        !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['security'])), 'integrazo_wz_error_log_nonce')
    ) {
        wp_send_json_error(['message' => __('Invalid security token.', 'integrazo-integration-for-zoho-apps')]);
    }
    if (! current_user_can('manage_options')) {
        wp_send_json_error(['message' => __('Insufficient permissions.', 'integrazo-integration-for-zoho-apps')]);
    }
    // Validate and sanitize the log ID
    $log_id = isset($_POST['log_id']) ? intval($_POST['log_id']) : 0;
    if (!$log_id) {
        wp_send_json_error(['message' => __('Invalid log ID.', 'integrazo-integration-for-zoho-apps')]);
    }

    // Fetch error log details
    $logs_model = new INTEGRAZO_WZ_Logs_Model();
    $integration_model = new INTEGRAZO_WZ_Integration_Model();

    $log_details = $logs_model->get_error_log($log_id);
    if (!$log_details) {
        wp_send_json_error(['message' => __('Log not found.', 'integrazo-integration-for-zoho-apps')]);
    }

    // Unserialize and enrich the log
    $log_details->submitted_data = maybe_unserialize($log_details->submitted_data);
    $log_details->response_data  = maybe_unserialize($log_details->response_data);
    $log_details->integration_name = $integration_model->get_integration_name($log_details->integration_id);

    // ✅ Return the response
    wp_send_json_success(['details' => $log_details]);
}
add_action('wp_ajax_INTEGRAZO_WZ_Error_Log_show_action', 'INTEGRAZO_WZ_Error_Log_show_action');