<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}


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

    if (empty($license_expiry_time)) {
        return; // No license set
    }
    $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/integrate-any-form-with-zoho-crm'); // 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', 'integrate-any-form-with-zoho-crm') . '</a>';
        $days_text = esc_html($days_to_expiry) . ' days';

        $message = esc_html__('Reminder: Your license will expire in ', 'integrate-any-form-with-zoho-crm') .
            $days_text .
            esc_html__('. Ensure uninterrupted access to premium features. ', 'integrate-any-form-with-zoho-crm') .
            $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', 'integrate-any-form-with-zoho-crm') . '</a>';
        $days_to_expiry_text = esc_html($days_to_expiry);

        $message = esc_html__('Attention: Your license will expire in ', 'integrate-any-form-with-zoho-crm') .
            $days_to_expiry_text .
            esc_html__(' day(s). Renew now to avoid losing access to premium features. ', 'integrate-any-form-with-zoho-crm') .
            $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', 'integrate-any-form-with-zoho-crm') . '</a>';

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

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