<?php
// Exit if accessed directly.
if (!defined('ABSPATH')) {
    exit; // Prevent direct access to the file.
}

// Hook to display the admin notice only on the plugin settings page
add_action('admin_notices', 'iafwzc_admin_review_notice');

function iafwzc_admin_review_notice()
{
    // Check if the review notice is enabled
    if (get_option('iafwzc_review_enabled') !== 'yes') {
        return; // Do not show if the option is not set to "yes"
    }

    // Show only on the "Integrate Any Form with Zoho CRM" settings page
    $current_screen = get_current_screen();
    if ($current_screen->id !== 'toplevel_page_integrate-any-form-with-zoho-crm') {
        return;
    }

    // Check if the user has permanently dismissed the notice
    if (get_option('iafwzc_review_dismissed') === 'yes') {
        return;
    }

    // Check if the user selected "Remind Me Later" and calculate the delay
    $remind_me_time = get_option('iafwzc_review_remind_me_later');
    if ($remind_me_time && $remind_me_time > (time() * 1000)) {
        return; // Do not show if the reminder time has not yet passed
    }
    // Generate a nonce for security
    $nonce = wp_create_nonce('iafwzc_review_nonce');
    // Review link (Modify this URL with your plugin's review page)
    $review_url = 'https://wordpress.org/plugins/integrate-any-form-with-zoho-crm/#reviews';

    echo '<div class="notice notice-info" id="iafwzc-review-notice">
        <p><strong>Help us improve Zoho CRM & Bigin Integration!</strong></p>
        <p>If you like our plugin, please take a moment to share your experience. Your feedback helps us enhance functionality and fix any issues.</p>
        <p>
            <a href="' . esc_url($review_url) . '" target="_blank">Give Review</a> | 
            <a href="#" id="iafwzc-review-later" data-nonce="' . esc_attr($nonce) . '">Remind Me Later</a> | 
            <a href="#" id="iafwzc-review-dismiss" data-nonce="' . esc_attr($nonce) . '">Already Provided</a>
        </p>
    </div>';
}

// Handle AJAX requests
add_action('wp_ajax_iafwzc_review_action', 'iafwzc_review_action');

function iafwzc_review_action()
{
    if (!isset($_POST['nonce'])) {
        wp_send_json_error(array('message' => 'Nonce is missing!'), 403);
    }

    // Properly unslash and sanitize the nonce before verification
    $nonce = sanitize_text_field(wp_unslash($_POST['nonce']));

    if (!wp_verify_nonce($nonce, 'iafwzc_review_nonce')) {
        wp_send_json_error(array('message' => 'Invalid nonce!'), 403);
    }

    if (!current_user_can('manage_options')) {
        wp_send_json_error(array('message' => 'Insufficient permissions.'), 403);
    }

    $action_type = sanitize_text_field(wp_unslash($_POST['action_type'] ?? ''));

    if (isset($_POST['action_type'])) {
        if ($action_type === 'dismiss') {
            update_option('iafwzc_review_dismissed', 'yes'); // Permanently dismiss
        } elseif ($action_type === 'later') {
            update_option('iafwzc_review_remind_me_later', time() * 1000 + (7 * DAY_IN_SECONDS * 1000)); // Store as milliseconds
        }
    }
    wp_send_json_success();
}

// Function to enable or disable review notice
function iafwzc_enable_review_notice($enable = 'yes')
{
    update_option('iafwzc_review_enabled', $enable);
}

// Uncomment the line below to enable or disable the notice
// iafwzc_enable_review_notice('yes'); // To enable
// iafwzc_enable_review_notice('no');  // To disable
