<?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', 'iafwzdesk_admin_review_notice');

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

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

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

    // Check if the user selected "Remind Me Later" and calculate the delay
    $remind_me_time = get_option('iafwzdesk_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('iafwzdesk_review_nonce');
    // Review link (Modify this URL with your plugin's review page)
    $review_url = 'https://wordpress.org/plugins/integrate-with-zoho-desk/#reviews';

    echo '<div class="notice notice-info" id="iafwzdesk-review-notice">
        <p><strong>Help us improve Zoho Desk 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="iafwzdesk-review-later" data-nonce="' . esc_attr($nonce) . '">Remind Me Later</a> | 
            <a href="#" id="iafwzdesk-review-dismiss" data-nonce="' . esc_attr($nonce) . '">Already Provided</a>
        </p>
    </div>';
}

// Handle AJAX requests
add_action('wp_ajax_iafwzdesk_review_action', 'iafwzdesk_review_action');

function iafwzdesk_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, 'iafwzdesk_review_nonce')) {
        wp_send_json_error(array('message' => 'Invalid nonce!'), 403);
    }

    if (isset($_POST['action_type'])) {
        if ($_POST['action_type'] === 'dismiss') {
            update_option('iafwzdesk_review_dismissed', 'yes'); // Permanently dismiss
        } elseif ($_POST['action_type'] === 'later') {
            update_option('iafwzdesk_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 iafwzdesk_enable_review_notice($enable = 'yes')
{
    update_option('iafwzdesk_review_enabled', $enable);
}
