<?php

if (! defined('ABSPATH')) {
    exit;
}


function integrazo_license_expiry_notice()
{
    $license_expiry_time = get_option('integrazo_fwcrm_form_session_key', null); // License expiry in milliseconds

    if (empty($license_expiry_time)) {
        return; // No license set
    }
    if ($license_expiry_time === "MTExMTExMTExMTExMQ==") {
        return;
    }
    $license_expiry_time = base64_decode($license_expiry_time, true);
    if ($license_expiry_time === false) {
        return; // Invalid base64 encoding
    }

    $current_time = round(microtime(true) * 1000); // Current time in milliseconds
    $purchase_url = esc_url('https://integrazo.com/products/freshworks-crm-integration-with-any-form?utm_source=plugin&utm_medium=license_renewal&utm_campaign=freshworks_integration'); // Replace with your store link

    $days_to_expiry = ceil(($license_expiry_time - $current_time) / (1000 * 60 * 60 * 24)); // Days until expiry

    if ($days_to_expiry > 7) {
        return; // License is valid and not within the warning period
    }

    if ($days_to_expiry <= 7 && $days_to_expiry > 2) {
        // Reminder: License will expire in 7 days
        $renew_link = '<a href="' . esc_url($purchase_url) . '" target="_blank" style="text-decoration: underline; font-weight: bold;">' . esc_html__('Renew Your License', 'crm-integration-freshworks-any-form') . '</a>';
        $days_text = esc_html($days_to_expiry) . ' days';

        $message = esc_html__('Reminder: Your license will expire in ', 'crm-integration-freshworks-any-form') .
            $days_text .
            esc_html__('. Ensure uninterrupted access to premium features. ', 'crm-integration-freshworks-any-form') .
            $renew_link;

        echo '<div class="notice notice-warning">
                <p>' . wp_kses_post($message) . '</p>
              </div>';
    } elseif ($days_to_expiry <= 2 && $days_to_expiry > 0) {
        // Critical Warning: License will expire within 2 days
        $renew_link = '<a href="' . esc_url($purchase_url) . '" target="_blank" style="text-decoration: underline; font-weight: bold;">' . esc_html__('Renew Your License', 'crm-integration-freshworks-any-form') . '</a>';
        $days_to_expiry_text = esc_html($days_to_expiry);

        $message = esc_html__('Attention: Your license will expire in ', 'crm-integration-freshworks-any-form') .
            $days_to_expiry_text .
            esc_html__(' day(s). Renew now to avoid losing access to premium features. ', 'crm-integration-freshworks-any-form') .
            $renew_link;

        echo '<div class="notice notice-error">
        <p>' . wp_kses_post($message) . '</p>
      </div>';
    } elseif ($days_to_expiry <= 0) {
        // Expired Warning: License expired
        $formatted_expiry_date = esc_html(date_i18n(get_option('date_format'), $license_expiry_time / 1000));
        $renew_link = '<a href="' . esc_url($purchase_url) . '" target="_blank" style="text-decoration: underline; font-weight: bold;">' . esc_html__('Renew Your License', 'crm-integration-freshworks-any-form') . '</a>';

        // Construct the message
        $message = esc_html__('Your license expired on ', 'crm-integration-freshworks-any-form') .
            $formatted_expiry_date .
            esc_html__('. Renew now to restore access to premium features and services. ', 'crm-integration-freshworks-any-form') .
            $renew_link;

        // Output the notice
        echo '<div class="notice notice-error">
                <p>' . wp_kses_post($message) . '</p>
              </div>';
    }
}
