<?php
/*
 * Test Agent Database Connection
 * Akses: http://localhost/mikhmon/test_agent_db.php
 */

error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "<h2>Test Agent Database Connection</h2>";

// Test 1: Include db_config
echo "<h3>1. Testing db_config.php include...</h3>";
if (file_exists('./include/db_config.php')) {
    echo "✓ File db_config.php exists<br>";
    include_once('./include/db_config.php');
    echo "✓ db_config.php included successfully<br>";
} else {
    echo "✗ File db_config.php NOT FOUND<br>";
    exit;
}

// Test 2: Test database connection
echo "<h3>2. Testing database connection...</h3>";
try {
    $conn = getDBConnection();
    if ($conn) {
        echo "✓ Database connected successfully<br>";
        echo "Database: " . DB_NAME . "<br>";
    } else {
        echo "✗ Database connection failed<br>";
        exit;
    }
} catch (Exception $e) {
    echo "✗ Error: " . $e->getMessage() . "<br>";
    exit;
}

// Test 3: Check tables
echo "<h3>3. Checking database tables...</h3>";
try {
    $tables = ['agents', 'agent_prices', 'agent_transactions'];
    foreach ($tables as $table) {
        $stmt = $conn->query("SHOW TABLES LIKE '$table'");
        if ($stmt->rowCount() > 0) {
            echo "✓ Table '$table' exists<br>";
        } else {
            echo "✗ Table '$table' NOT FOUND<br>";
        }
    }
} catch (Exception $e) {
    echo "✗ Error checking tables: " . $e->getMessage() . "<br>";
}

// Test 4: Test Agent class
echo "<h3>4. Testing Agent class...</h3>";
if (file_exists('./lib/Agent.class.php')) {
    echo "✓ File Agent.class.php exists<br>";
    include_once('./lib/Agent.class.php');
    echo "✓ Agent.class.php included successfully<br>";
    
    try {
        $agent = new Agent();
        echo "✓ Agent class instantiated successfully<br>";
        
        $agents = $agent->getAllAgents();
        echo "✓ getAllAgents() executed successfully<br>";
        echo "Total agents: " . count($agents) . "<br>";
    } catch (Exception $e) {
        echo "✗ Error with Agent class: " . $e->getMessage() . "<br>";
    }
} else {
    echo "✗ File Agent.class.php NOT FOUND<br>";
}

echo "<h3>✓ All tests completed!</h3>";
echo "<p><a href='./admin.php'>Go to MikhMon Admin</a></p>";
?>
