<?php

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

function iafwzcai_license_expiry_notice()
{
    $license_expiry_time = get_option('iafwzcai_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-with-zoho-campaigns'); // 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-with-zoho-campaigns') . '</a>';
        $days_text = esc_html($days_to_expiry) . ' days';

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

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

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

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