<?php
// Test script to check current site settings
session_start();
$_SESSION['user_id'] = 1; // Simulate logged in user

require_once 'config/database.php';
require_once 'includes/site_settings.php';

echo "<h2>Current Site Settings Test</h2>\n";

// Test database connection
try {
    $pdo->query("SELECT 1");
    echo "<p style='color: green;'>✓ Database connection successful</p>\n";
} catch (Exception $e) {
    echo "<p style='color: red;'>✗ Database connection failed: " . $e->getMessage() . "</p>\n";
    exit;
}

// Check if site_settings table exists
try {
    $stmt = $pdo->query("SHOW TABLES LIKE 'site_settings'");
    if ($stmt->rowCount() > 0) {
        echo "<p style='color: green;'>✓ site_settings table exists</p>\n";
    } else {
        echo "<p style='color: orange;'>⚠ site_settings table does not exist (will be created when saving settings)</p>\n";
    }
} catch (Exception $e) {
    echo "<p style='color: red;'>✗ Error checking table: " . $e->getMessage() . "</p>\n";
}

// Fetch current settings
$site_name = get_setting($pdo, 'site_name', 'Not Set');
$site_address = get_setting($pdo, 'site_address', 'Not Set');

echo "<h3>Current Settings:</h3>\n";
echo "<p><strong>Site Name:</strong> " . htmlspecialchars($site_name) . "</p>\n";
echo "<p><strong>Site Address:</strong> " . htmlspecialchars($site_address) . "</p>\n";

// Test all settings function
$all_settings = get_all_settings($pdo);
echo "<h3>All Settings (" . count($all_settings) . " found):</h3>\n";
if (empty($all_settings)) {
    echo "<p>No settings found in database</p>\n";
} else {
    echo "<ul>\n";
    foreach ($all_settings as $key => $value) {
        echo "<li><strong>" . htmlspecialchars($key) . ":</strong> " . htmlspecialchars($value) . "</li>\n";
    }
    echo "</ul>\n";
}

echo "<br><p><a href='admin/site_settings.php'>Go to Site Settings Admin</a> | <a href='expenses/income_expense_report.php'>Go to Report</a></p>\n";
?>