<?php
/**
 * Diagnose UddoktaPay Configuration
 * 
 * This script provides detailed diagnostics for UddoktaPay configuration issues
 */

require_once 'config/database.php';

echo "=== UddoktaPay Configuration Diagnosis ===\n\n";

// Check if we can connect to the database
echo "1. Database connection test...\n";
try {
    if (isset($pdo)) {
        echo "   ✓ Database connection successful\n";
    } else {
        echo "   ✗ Database connection failed - \$pdo not set\n";
        exit(1);
    }
} catch (Exception $e) {
    echo "   ✗ Database connection failed: " . $e->getMessage() . "\n";
    exit(1);
}

// Check if settings table exists
echo "\n2. Checking for settings table...\n";
try {
    $stmt = $pdo->query("SHOW TABLES LIKE 'settings'");
    $result = $stmt->fetchAll();
    if (count($result) > 0) {
        echo "   ✓ Settings table exists\n";
    } else {
        echo "   ✗ Settings table does not exist\n";
        exit(1);
    }
} catch (Exception $e) {
    echo "   ✗ Error checking settings table: " . $e->getMessage() . "\n";
    exit(1);
}

// Get all settings
echo "\n3. Retrieving all settings...\n";
try {
    $stmt = $pdo->query("SELECT setting_key, setting_value FROM settings");
    $results = $stmt->fetchAll();
    
    if (count($results) > 0) {
        echo "   Found " . count($results) . " settings:\n";
        foreach ($results as $row) {
            echo "     " . $row['setting_key'] . " = " . $row['setting_value'] . "\n";
        }
    } else {
        echo "   No settings found in database\n";
    }
} catch (Exception $e) {
    echo "   ✗ Error retrieving settings: " . $e->getMessage() . "\n";
    exit(1);
}

// Get UddoktaPay specific settings
echo "\n4. Retrieving UddoktaPay specific settings...\n";
try {
    $stmt = $pdo->query("SELECT setting_key, setting_value FROM settings WHERE setting_key LIKE 'uddoktapay_%'");
    $results = $stmt->fetchAll();
    
    if (count($results) > 0) {
        echo "   Found " . count($results) . " UddoktaPay settings:\n";
        foreach ($results as $row) {
            echo "     " . $row['setting_key'] . " = " . $row['setting_value'] . "\n";
        }
    } else {
        echo "   No UddoktaPay settings found in database\n";
    }
} catch (Exception $e) {
    echo "   ✗ Error retrieving UddoktaPay settings: " . $e->getMessage() . "\n";
    exit(1);
}

// Check if we can include the config file
echo "\n5. Testing config file inclusion...\n";
if (file_exists('config/uddoktapay.php')) {
    echo "   ✓ config/uddoktapay.php exists\n";
    
    // Try to include and test functions
    include 'config/uddoktapay.php';
    
    if (function_exists('get_uddoktapay_config')) {
        echo "   ✓ get_uddoktapay_config function exists\n";
        $config = get_uddoktapay_config();
        echo "   Configuration from function:\n";
        foreach ($config as $key => $value) {
            echo "     " . $key . " = " . (is_bool($value) ? ($value ? 'true' : 'false') : $value) . "\n";
        }
    } else {
        echo "   ✗ get_uddoktapay_config function not found\n";
    }
    
    if (function_exists('validate_uddoktapay_config')) {
        echo "   ✓ validate_uddoktapay_config function exists\n";
        $validation = validate_uddoktapay_config();
        echo "   Validation result: " . json_encode($validation, JSON_UNESCAPED_UNICODE) . "\n";
    } else {
        echo "   ✗ validate_uddoktapay_config function not found\n";
    }
} else {
    echo "   ✗ config/uddoktapay.php does not exist\n";
}

echo "\n=== Diagnosis Complete ===\n";
?>