<?php
/**
 * Test script to verify Versement API endpoints
 */

echo "=== Testing Versement API ===" . PHP_EOL;

// Test 1: Check if TypeVehicule model exists
echo PHP_EOL . "1. Checking TypeVehicule model..." . PHP_EOL;
if (file_exists('models/TypeVehicule.php')) {
    require_once 'models/TypeVehicule.php';
    echo "✅ TypeVehicule model loaded successfully" . PHP_EOL;
} else {
    echo "❌ TypeVehicule model not found" . PHP_EOL;
}

// Test 2: Check if Tarif model exists
echo PHP_EOL . "2. Checking Tarif model..." . PHP_EOL;
if (file_exists('models/Tarif.php')) {
    require_once 'models/Tarif.php';
    echo "✅ Tarif model loaded successfully" . PHP_EOL;
} else {
    echo "❌ Tarif model not found" . PHP_EOL;
}

// Test 3: Check if BonVers model exists
echo PHP_EOL . "3. Checking BonVers model..." . PHP_EOL;
if (file_exists('models/BonVers.php')) {
    require_once 'models/BonVers.php';
    echo "✅ BonVers model loaded successfully" . PHP_EOL;
} else {
    echo "❌ BonVers model not found" . PHP_EOL;
}

// Test 4: Check if Versement controller exists
echo PHP_EOL . "4. Checking Versement controller..." . PHP_EOL;
if (file_exists('controllers/VersementController.php')) {
    require_once 'controllers/VersementController.php';
    echo "✅ VersementController loaded successfully" . PHP_EOL;
} else {
    echo "❌ VersementController not found" . PHP_EOL;
}

// Test 5: Check if index.php includes Versement routes
echo PHP_EOL . "5. Checking Versement routes in index.php..." . PHP_EOL;
if (file_exists('index.php')) {
    $content = file_get_contents('index.php');
    if (strpos($content, 'handleVersementRoutes') !== false && strpos($content, 'versement') !== false) {
        echo "✅ Versement routes found in index.php" . PHP_EOL;
    } else {
        echo "❌ Versement routes not found in index.php" . PHP_EOL;
    }
} else {
    echo "❌ index.php not found" . PHP_EOL;
}

// Test 6: Check database configuration
echo PHP_EOL . "6. Checking database configuration..." . PHP_EOL;
if (file_exists('config/Database.php')) {
    require_once 'config/Database.php';
    try {
        $db = new Database();
        $conn = $db->getConnection();
        echo "✅ Database connection established successfully" . PHP_EOL;
        
        // Test if we can connect to the database
        $stmt = $conn->prepare("SELECT 1");
        $stmt->execute();
        echo "✅ Database query test passed" . PHP_EOL;
        
    } catch (Exception $e) {
        echo "❌ Database connection failed: " . $e->getMessage() . PHP_EOL;
    }
} else {
    echo "❌ Database configuration not found" . PHP_EOL;
}

// Test 7: Check if operators table exists
echo PHP_EOL . "7. Checking operators table..." . PHP_EOL;
try {
    if (isset($conn) && $conn) {
        $stmt = $conn->prepare("SHOW TABLES LIKE 'operators'");
        $stmt->execute();
        if ($stmt->rowCount() > 0) {
            echo "✅ Operators table exists" . PHP_EOL;
            
            // Check if type column exists
            $stmt = $conn->prepare("SHOW COLUMNS FROM operators LIKE 'type'");
            $stmt->execute();
            if ($stmt->rowCount() > 0) {
                echo "✅ Type column exists in operators table" . PHP_EOL;
            } else {
                echo "⚠️ Type column not found in operators table" . PHP_EOL;
            }
        } else {
            echo "❌ Operators table not found" . PHP_EOL;
        }
    }
} catch (Exception $e) {
    echo "❌ Error checking operators table: " . $e->getMessage() . PHP_EOL;
}

// Test 8: Check if buses table exists with required columns
echo PHP_EOL . "8. Checking buses table..." . PHP_EOL;
try {
    if (isset($conn) && $conn) {
        $stmt = $conn->prepare("SHOW TABLES LIKE 'buses'");
        $stmt->execute();
        if ($stmt->rowCount() > 0) {
            echo "✅ Buses table exists" . PHP_EOL;
            
            // Check if id_operateur and type columns exist
            $stmt = $conn->prepare("SHOW COLUMNS FROM buses LIKE 'id_operateur' OR Field LIKE 'type' OR Field LIKE 'matricule'");
            $stmt->execute();
            $columns = $stmt->fetchAll(PDO::FETCH_ASSOC);
            $requiredColumns = ['id_operateur', 'type', 'matricule'];
            $foundColumns = array_column($columns, 'Field');
            
            foreach ($requiredColumns as $column) {
                if (in_array($column, $foundColumns)) {
                    echo "✅ " . $column . " column exists" . PHP_EOL;
                } else {
                    echo "❌ " . $column . " column not found" . PHP_EOL;
                }
            }
        } else {
            echo "❌ Buses table not found" . PHP_EOL;
        }
    }
} catch (Exception $e) {
    echo "❌ Error checking buses table: " . $e->getMessage() . PHP_EOL;
}

echo PHP_EOL . "=== API Structure Test Complete ===" . PHP_EOL;
?>