<?php
/**
 * Debug script to check UddoktaPay configuration
 */
session_start();
require_once '../config/database.php';
require_once '../config/uddoktapay.php';

echo "<h1>UddoktaPay Configuration Debug</h1>";

// Check if user is admin
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'admin') {
    echo "<p>You need to be logged in as admin to view this page.</p>";
    echo "<p><a href='../login.php'>Login</a></p>";
    exit();
}

echo "<h2>Database Settings</h2>";
try {
    // Load settings from database
    $stmt = $pdo->query("SELECT setting_key, setting_value FROM settings WHERE setting_key LIKE 'uddoktapay_%'");
    $settings = $stmt->fetchAll();
    
    if (empty($settings)) {
        echo "<p style='color: orange;'>⚠ No UddoktaPay settings found in database</p>";
        echo "<p>Please go to <a href='../admin/uddoktapay_settings.php'>UddoktaPay Settings</a> to configure.</p>";
    } else {
        echo "<table border='1' cellpadding='5' cellspacing='0'>";
        echo "<tr><th>Setting Key</th><th>Setting Value</th></tr>";
        foreach ($settings as $setting) {
            echo "<tr>";
            echo "<td>" . htmlspecialchars($setting['setting_key']) . "</td>";
            echo "<td>" . htmlspecialchars($setting['setting_value']) . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    }
} catch (Exception $e) {
    echo "<p style='color: red;'>✗ Database error: " . $e->getMessage() . "</p>";
}

echo "<h2>Configuration Function Output</h2>";
try {
    $config = get_uddoktapay_config();
    echo "<pre>" . print_r($config, true) . "</pre>";
    
    echo "<h3>Configuration Status</h3>";
    echo "<p>Enabled: " . ($config['enabled'] ? 'Yes' : 'No') . "</p>";
    echo "<p>API Key Set: " . (!empty($config['api_key']) ? 'Yes' : 'No') . "</p>";
    echo "<p>Base URL: " . htmlspecialchars($config['base_url']) . "</p>";
    
    if ($config['enabled'] && !empty($config['api_key'])) {
        echo "<p style='color: green;'>✓ UddoktaPay is properly configured and enabled</p>";
    } else {
        echo "<p style='color: orange;'>⚠ UddoktaPay is not properly configured or not enabled</p>";
        echo "<p>To fix this:</p>";
        echo "<ol>";
        echo "<li>Go to <a href='../admin/uddoktapay_settings.php'>Admin Panel → UddoktaPay Settings</a></li>";
        echo "<li>Enter your UddoktaPay API Key</li>";
        echo "<li>Make sure 'UddoktaPay সক্রিয় করুন' is checked</li>";
        echo "<li>Click 'সেটিংস সংরক্ষণ করুন'</li>";
        echo "</ol>";
    }
} catch (Exception $e) {
    echo "<p style='color: red;'>✗ Configuration function error: " . $e->getMessage() . "</p>";
}

echo "<h2>API URL Test</h2>";
try {
    $api_url = get_uddoktapay_api_url('/api/checkout-v2');
    echo "<p>API URL: " . htmlspecialchars($api_url) . "</p>";
} catch (Exception $e) {
    echo "<p style='color: red;'>✗ API URL error: " . $e->getMessage() . "</p>";
}

echo "<p><a href='my_fees.php'>Go to My Fees</a> | <a href='../admin/uddoktapay_settings.php'>Configure UddoktaPay</a></p>";
?>