<?php
/**
 * Activation Toast Notifications Handler
 * Displays toast notifications for activation events (WAF sync, etc.)
 *
 * @package VMPFence
 * @since 2.2.2
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Show WAF sync toast notification after activation
 * 
 * Hooks into admin_enqueue_scripts to display toast notification
 * when user first visits VMPFence admin page after activation
 */
function vmpfence_show_waf_sync_toast() {
    // Show on WordPress dashboard or VMPFence admin pages
    $screen = get_current_screen();
    if (!$screen) {
        return;
    }
    
    // Allow on dashboard or VMPFence pages
    $is_dashboard = ($screen->id === 'dashboard');
    $is_vmpfence_page = (stripos($screen->id, 'vmpfence') !== false);
    
    if (!$is_dashboard && !$is_vmpfence_page) {
        return;
    }
    
    // Check if we have a sync result to display
    $sync_result = get_transient('vmpfence_waf_sync_result');
    if (!$sync_result || !is_array($sync_result)) {
        return;
    }
    
    // Delete transient so we only show once
    delete_transient('vmpfence_waf_sync_result');
    
    // Enqueue toast notification system
    wp_enqueue_script(
        'vmpfence-toast-notifications',
        VMPFENCE_PLUGIN_URL . 'admin/assets/js/toast-notifications.js',
        array('jquery'),
        VMPFENCE_VERSION,
        true
    );
    
    // Enqueue toast CSS
    wp_enqueue_style(
        'vmpfence-toast-notifications',
        VMPFENCE_PLUGIN_URL . 'admin/assets/css/toast-notifications.css',
        array(),
        VMPFENCE_VERSION
    );
    
    // Output inline script to show toast
    $inline_script = '';
    
    if (!empty($sync_result['success'])) {
        // Validate required keys exist with defaults
        $total_rules = isset($sync_result['total_rules']) ? (int)$sync_result['total_rules'] : 0;
        $new_rules = isset($sync_result['new_rules']) ? (int)$sync_result['new_rules'] : 0;
        $updated_rules = isset($sync_result['updated_rules']) ? (int)$sync_result['updated_rules'] : 0;
        
        // Build message with HTML formatting in JavaScript (not PHP) to avoid esc_js() breaking tags
        $inline_script = "
        jQuery(document).ready(function($) {
            if (typeof VMPFenceNotifications !== 'undefined') {
                VMPFenceNotifications.init();
                setTimeout(function() {
                    var message = 'Successfully synced <strong>" . (int)$total_rules . " WAF rules</strong> from the cloud (" . (int)$new_rules . " new, " . (int)$updated_rules . " updated)';
                    VMPFenceNotifications.showSuccess(
                        message,
                        6000,
                        'Firewall Rules Synced'
                    );
                }, 500);
            }
        });
        ";
    } else {
        // Extract error message with fallback  
        $error_detail = isset($sync_result['error']) ? esc_js($sync_result['error']) : 'Unknown error';
        
        // Build message with HTML formatting in JavaScript to avoid esc_js() breaking tags
        $inline_script = "
        jQuery(document).ready(function($) {
            if (typeof VMPFenceNotifications !== 'undefined') {
                VMPFenceNotifications.init();
                setTimeout(function() {
                    var message = 'Failed to sync WAF rules from cloud. <strong>Action Required:</strong> Please manually sync from Firewall page or wait for automatic hourly sync.';
                    VMPFenceNotifications.showWarning(
                        message,
                        10000,
                        'WAF Sync Failed'
                    );
                }, 500);
            }
        });
        ";
    }
    
    wp_add_inline_script('vmpfence-toast-notifications', $inline_script, 'after');
}
add_action('admin_enqueue_scripts', 'vmpfence_show_waf_sync_toast', 999);
