<?php
session_start();
require_once '../config/database.php';

// Check if user is admin
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'admin') {
    header('Location: ../login.php');
    exit();
}

echo "<h1>Database Settings Check</h1>";

try {
    // Check if settings table exists
    $stmt = $pdo->query("SHOW TABLES LIKE 'settings'");
    $tableExists = $stmt->fetch();
    
    if (!$tableExists) {
        echo "<p style='color: red;'>Settings table does not exist!</p>";
        exit();
    }
    
    echo "<p style='color: green;'>✓ Settings table exists</p>";
    
    // Check UddoktaPay settings
    $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</p>";
        echo "<p>Please go to <a href='../admin/uddoktapay_settings.php'>UddoktaPay Settings</a> to configure.</p>";
    } else {
        echo "<p style='color: green;'>✓ Found " . count($settings) . " UddoktaPay settings</p>";
        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>";
    }
    
    // Check all settings
    echo "<h2>All Settings</h2>";
    $stmt = $pdo->query("SELECT setting_key, setting_value FROM settings");
    $allSettings = $stmt->fetchAll();
    
    echo "<table border='1' cellpadding='5' cellspacing='0'>";
    echo "<tr><th>Setting Key</th><th>Setting Value</th></tr>";
    foreach ($allSettings 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;'>Error: " . $e->getMessage() . "</p>";
}

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