<?php
// This is a diagnostic script to check webhook configuration
// Save this as webhook-test.php and run it to verify your setup

// Set appropriate headers
header('Content-Type: text/plain');

// Display environment info
echo "======= WEBHOOK CONFIGURATION CHECK =======\n\n";

// Check if Paystack webhook URL is properly configured
$expected_webhook_url = "https://yourdomain.com/callback.php";
echo "Expected webhook URL: $expected_webhook_url\n";
echo "Make sure this URL is configured in your Paystack dashboard.\n\n";

// Check if webhook secrets are set
if (file_exists('.env')) {
    echo "✅ .env file exists\n";
    
    // Check if we can load the .env file
    if (class_exists('Dotenv\Dotenv')) {
        echo "✅ Dotenv class exists\n";
        
        // Try loading the .env file
        try {
            $dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
            $dotenv->load();
            echo "✅ .env file loaded successfully\n";
            
            // Check if PAYSTACK_SECRET_KEY is set
            if (isset($_ENV['PAYSTACK_SECRET_KEY']) && !empty($_ENV['PAYSTACK_SECRET_KEY'])) {
                echo "✅ PAYSTACK_SECRET_KEY is set\n";
                // Don't print the actual key for security reasons
                echo "   Key length: " . strlen($_ENV['PAYSTACK_SECRET_KEY']) . " characters\n";
            } else {
                echo "❌ PAYSTACK_SECRET_KEY is not set or empty\n";
            }
        } catch (Exception $e) {
            echo "❌ Error loading .env file: " . $e->getMessage() . "\n";
        }
    } else {
        echo "❌ Dotenv class not found. Make sure vendor/autoload.php is included.\n";
    }
} else {
    echo "❌ .env file not found\n";
}

// Check for webhook logs
$webhook_log = "paystack_webhook.log";
if (file_exists($webhook_log)) {
    echo "\n✅ Webhook log file exists\n";
    $log_size = filesize($webhook_log);
    echo "   Log size: " . number_format($log_size) . " bytes\n";
    
    // Check if log has recent entries
    $last_modified = filemtime($webhook_log);
    $time_diff = time() - $last_modified;
    echo "   Last modified: " . date("Y-m-d H:i:s", $last_modified) . " (" . 
         floor($time_diff / 86400) . " days, " . 
         floor(($time_diff % 86400) / 3600) . " hours ago)\n";
    
    // Check last few entries
    echo "\nLast 5 webhook log entries:\n";
    $log_entries = file($webhook_log, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    $log_entries = array_slice($log_entries, -10);
    foreach ($log_entries as $entry) {
        // Don't display full log content, just check if transfer events exist
        if (strpos($entry, '"event":"transfer') !== false) {
            echo "   - Found transfer event in logs\n";
        }
    }
    
    // Check specifically for transfer events
    $log_content = file_get_contents($webhook_log);
    if (strpos($log_content, '"event":"transfer.success"') !== false) {
        echo "\n✅ Found 'transfer.success' events in log\n";
    } else {
        echo "\n❌ No 'transfer.success' events found in log\n";
    }
} else {
    echo "\n❌ Webhook log file not found\n";
}

// Check payout log
$payout_log = "paystack_payouts.log";
if (file_exists($payout_log)) {
    echo "\n✅ Payout log file exists\n";
    echo "Last 5 payout log entries:\n";
    $log_entries = file($payout_log, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    $log_entries = array_slice($log_entries, -5);
    foreach ($log_entries as $entry) {
        echo "   $entry\n";
    }
} else {
    echo "\n❌ Payout log file not found\n";
}

// Check mail configuration
echo "\nMail Configuration:\n";
$mail_config = ini_get('sendmail_path');
if (!empty($mail_config)) {
    echo "✅ sendmail_path: $mail_config\n";
} else {
    echo "❓ sendmail_path not configured, might be using PHP mail() function\n";
}

// Mail function check
if (function_exists('mail')) {
    echo "✅ mail() function exists\n";
} else {
    echo "❌ mail() function not available\n";
}

// Test if we can write to log
echo "\nTesting log writing capabilities:\n";
$test_log = "webhook_test_" . time() . ".log";
if (file_put_contents($test_log, "Test log entry created at " . date("Y-m-d H:i:s") . "\n")) {
    echo "✅ Successfully wrote to test log file\n";
    unlink($test_log);
} else {
    echo "❌ Failed to write to test log file\n";
}

echo "\n======= END OF CONFIGURATION CHECK =======\n";
?>