<?php
require_once 'config/database.php';
require_once 'includes/zashof.php';

echo "<h1>Zashof Configuration Verification</h1>";

try {
    // Test configuration loading
    echo "<h2>Database Configuration</h2>";
    $stmt = $pdo->query("SELECT setting_key, setting_value FROM settings WHERE setting_key LIKE 'zashof_%'");
    $settings = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
    
    if (empty($settings)) {
        echo "<p style='color: red;'>No Zashof settings found in database</p>";
    } else {
        echo "<ul>";
        foreach ($settings as $key => $value) {
            // For API key, show only first and last few characters
            $display_value = $value;
            if ($key === 'zashof_api_key' && strlen($value) > 20) {
                $display_value = substr($value, 0, 10) . '...' . substr($value, -10);
            }
            echo "<li><strong>" . htmlspecialchars($key) . ":</strong> " . htmlspecialchars($display_value) . "</li>";
        }
        echo "</ul>";
    }
    
    // Test Zashof class
    echo "<h2>ZashofPayment Class Test</h2>";
    $zashof = new ZashofPayment();
    
    // Test if enabled
    $enabled = $zashof->isEnabled();
    echo "<p>Enabled: " . ($enabled ? '<span style="color: green;">Yes</span>' : '<span style="color: red;">No</span>') . "</p>";
    
    if ($enabled) {
        echo "<p style='color: green;'>✓ Zashof payment is enabled and properly configured</p>";
        
        // Test configuration status
        $configStatus = $zashof->getConfigStatus();
        echo "<h3>Configuration Status:</h3>";
        echo "<ul>";
        echo "<li>Enabled: " . ($configStatus['enabled'] ? '<span style="color: green;">Yes</span>' : '<span style="color: red;">No</span>') . "</li>";
        echo "<li>API Key Set: " . ($configStatus['api_key_set'] ? '<span style="color: green;">Yes</span>' : '<span style="color: red;">No</span>') . "</li>";
        echo "<li>Base URL: " . htmlspecialchars($configStatus['base_url']) . "</li>";
        echo "<li>Create Charge URL: " . htmlspecialchars($configStatus['create_charge_url']) . "</li>";
        echo "<li>Verify Payment URL: " . htmlspecialchars($configStatus['verify_payment_url']) . "</li>";
        echo "</ul>";
    } else {
        echo "<p style='color: orange;'>⚠ Zashof payment is disabled or not properly configured</p>";
        
        // Show detailed config status
        $configStatus = $zashof->getConfigStatus();
        echo "<h3>Detailed Configuration Status:</h3>";
        echo "<ul>";
        echo "<li>Enabled: " . ($configStatus['enabled'] ? '<span style="color: green;">Yes</span>' : '<span style="color: red;">No</span>') . "</li>";
        echo "<li>API Key Set: " . ($configStatus['api_key_set'] ? '<span style="color: green;">Yes</span>' : '<span style="color: red;">No</span>') . "</li>";
        echo "<li>Base URL Set: " . (!empty($configStatus['base_url']) ? '<span style="color: green;">Yes</span>' : '<span style="color: red;">No</span>') . "</li>";
        echo "</ul>";
    }
    
} catch (Exception $e) {
    echo "<p style='color: red;'>✗ Error: " . $e->getMessage() . "</p>";
}

echo "<h2>Next Steps</h2>";
echo "<ol>";
echo "<li>Go to <a href='admin/zashof_settings.php'>Admin Panel → Zashof Payment Settings</a></li>";
echo "<li>Verify your Zashof API credentials are correct</li>";
echo "<li>If credentials are correct, contact Zashof support about the API key authorization issue</li>";
echo "<li>Check that the API key is activated and not restricted</li>";
echo "</ol>";

echo "<p><a href='fees/my_fees.php' class='btn btn-primary'>Go to My Fees</a> | <a href='admin/zashof_settings.php' class='btn btn-secondary'>Configure Zashof Payment</a></p>";
?>