<?php
/**
 * Check Notification Settings
 * 
 * Usage: Visit /wp-content/plugins/woonoow/check-settings.php
 */

// Load WordPress
require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/wp-load.php';

if (!current_user_can('manage_options')) {
    die('Access denied');
}

header('Content-Type: text/plain; charset=utf-8');

echo "=== WooNooW Notification Settings Check ===\n\n";

// 1. System Mode
echo "1. Notification System Mode:\n";
$system_mode = get_option('woonoow_notification_system_mode', 'not set');
echo "   Value: $system_mode\n";
if ($system_mode === 'woonoow') {
    echo "   ✓ WooNooW notifications enabled\n";
} elseif ($system_mode === 'woocommerce') {
    echo "   ✗ Using WooCommerce default emails (WooNooW disabled)\n";
} else {
    echo "   ⚠ Not set (defaulting to WooNooW)\n";
}

// 2. Email Channel
echo "\n2. Email Channel:\n";
$email_enabled = get_option('woonoow_email_notifications_enabled', 'not set');
echo "   Value: " . var_export($email_enabled, true) . "\n";
if ($email_enabled) {
    echo "   ✓ Email notifications enabled\n";
} else {
    echo "   ✗ Email notifications disabled\n";
}

// 3. Notification Settings
echo "\n3. Event Configuration:\n";
$settings = get_option('woonoow_notification_settings', []);

if (empty($settings)) {
    echo "   ✗ No notification settings found!\n";
    echo "   This means no events are configured.\n";
} else {
    echo "   ✓ Settings found\n";
    
    if (isset($settings['events'])) {
        echo "\n   Events:\n";
        foreach ($settings['events'] as $event_id => $event_data) {
            echo "   - $event_id:\n";
            
            if (isset($event_data['channels']['email'])) {
                $email_config = $event_data['channels']['email'];
                $enabled = $email_config['enabled'] ?? false;
                $status = $enabled ? '✓ ENABLED' : '✗ DISABLED';
                echo "     Email: $status\n";
                
                if (isset($email_config['recipients'])) {
                    echo "     Recipients: " . implode(', ', array_keys($email_config['recipients'])) . "\n";
                }
            } else {
                echo "     Email: ✗ NOT CONFIGURED\n";
            }
        }
    } else {
        echo "   ⚠ No events configured\n";
    }
}

// 4. Email Templates
echo "\n4. Email Templates:\n";
$templates = get_option('woonoow_email_templates', []);

if (empty($templates)) {
    echo "   ✗ No email templates found!\n";
} else {
    echo "   ✓ " . count($templates) . " template(s) found\n";
    foreach ($templates as $template_id => $template_data) {
        echo "   - $template_id: " . ($template_data['subject'] ?? 'no subject') . "\n";
    }
}

// 5. WooCommerce Hooks
echo "\n5. WooCommerce Hook Status:\n";
global $wp_filter;

$hooks_to_check = [
    'woocommerce_order_status_pending_to_processing',
    'woocommerce_order_status_pending_to_completed',
    'woocommerce_order_status_processing_to_completed',
    'woocommerce_order_status_completed',
];

foreach ($hooks_to_check as $hook) {
    if (isset($wp_filter[$hook])) {
        $callbacks = $wp_filter[$hook]->callbacks;
        $count = 0;
        foreach ($callbacks as $priority => $funcs) {
            $count += count($funcs);
        }
        echo "   ✓ $hook: $count callback(s)\n";
    } else {
        echo "   ✗ $hook: NOT REGISTERED\n";
    }
}

// 6. Mail Queue Hook
echo "\n6. Mail Queue Hook:\n";
if (isset($wp_filter['woonoow/mail/send'])) {
    echo "   ✓ woonoow/mail/send: REGISTERED\n";
} else {
    echo "   ✗ woonoow/mail/send: NOT REGISTERED\n";
    echo "   This means MailQueue::init() was not called!\n";
}

// 7. Action Scheduler
echo "\n7. Action Scheduler:\n";
if (function_exists('as_enqueue_async_action')) {
    echo "   ✓ Available\n";
    
    global $wpdb;
    $stats = [
        'pending' => $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}actionscheduler_actions WHERE hook = 'woonoow/mail/send' AND status = 'pending'"),
        'complete' => $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}actionscheduler_actions WHERE hook = 'woonoow/mail/send' AND status = 'complete'"),
        'failed' => $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}actionscheduler_actions WHERE hook = 'woonoow/mail/send' AND status = 'failed'"),
    ];
    
    echo "   Pending: {$stats['pending']}\n";
    echo "   Complete: {$stats['complete']}\n";
    echo "   Failed: {$stats['failed']}\n";
} else {
    echo "   ✗ Not available (using wp-cron)\n";
}

// 8. Queued Emails
echo "\n8. Queued Emails (wp_options):\n";
global $wpdb;
$queued = $wpdb->get_results(
    "SELECT option_name, LENGTH(option_value) as size FROM {$wpdb->options} 
    WHERE option_name LIKE 'woonoow_mail_%' 
    ORDER BY option_id DESC LIMIT 10"
);

if (empty($queued)) {
    echo "   ⚠ No emails in queue\n";
} else {
    echo "   " . count($queued) . " email(s) in queue:\n";
    foreach ($queued as $row) {
        echo "   - {$row->option_name} ({$row->size} bytes)\n";
    }
}

echo "\n=== Summary ===\n";
echo "System Mode: $system_mode\n";
echo "Email Channel: " . ($email_enabled ? 'enabled' : 'disabled') . "\n";
echo "Events Configured: " . (isset($settings['events']) ? count($settings['events']) : 0) . "\n";
echo "Templates: " . count($templates) . "\n";
echo "Queued Emails: " . count($queued) . "\n";

echo "\n=== Recommendations ===\n";
if ($system_mode !== 'woonoow') {
    echo "⚠ Set notification system mode to 'woonoow'\n";
}
if (!$email_enabled) {
    echo "⚠ Enable email channel\n";
}
if (empty($settings['events'])) {
    echo "⚠ Configure events in notification settings\n";
}
if (!isset($wp_filter['woonoow/mail/send'])) {
    echo "⚠ MailQueue hook not registered - check Bootstrap::init()\n";
}

echo "\nDone!\n";
