<?php
/**
 * Script de test de la connexion à la base de données des adresses
 */

require_once __DIR__ . '/src/Config/AppConfig.php';
require_once __DIR__ . '/src/Core/AddressesDatabase.php';

echo "Test de connexion à la base de données des adresses\n";
echo "==================================================\n\n";

try {
    // Initialiser la configuration
    $appConfig = AppConfig::getInstance();
    $addressesConfig = $appConfig->getAddressesDatabaseConfig();
    
    echo "Configuration:\n";
    echo "- Environnement: " . $appConfig->getEnvironment() . "\n";
    echo "- Host: " . $addressesConfig['host'] . "\n";
    echo "- Database: " . $addressesConfig['name'] . "\n";
    echo "- Username: " . $addressesConfig['username'] . "\n\n";
    
    // Initialiser la connexion
    AddressesDatabase::init($addressesConfig);
    $db = AddressesDatabase::getInstance();
    
    echo "✓ Connexion réussie!\n\n";
    
    // Tester une requête simple
    echo "Test de requête...\n";
    $stmt = $db->query("SELECT COUNT(*) as total FROM adresses LIMIT 1");
    $result = $stmt->fetch();
    echo "✓ Nombre total d'adresses: " . number_format($result['total']) . "\n\n";
    
    // Tester les fonctions géospatiales
    echo "Test des fonctions géospatiales...\n";
    $stmt = $db->query("SELECT ST_AsText(ST_GeomFromText('POINT(2.3522 48.8566)', 4326)) as point");
    $result = $stmt->fetch();
    echo "✓ Fonctions géospatiales disponibles: " . $result['point'] . "\n\n";
    
    // Afficher les colonnes de la table adresses
    echo "Structure de la table adresses:\n";
    $stmt = $db->query("DESCRIBE adresses");
    $columns = $stmt->fetchAll(PDO::FETCH_ASSOC);
    foreach ($columns as $column) {
        echo "- " . $column['Field'] . " (" . $column['Type'] . ")\n";
    }
    
} catch (Exception $e) {
    echo "✗ Erreur: " . $e->getMessage() . "\n";
    echo "Trace:\n" . $e->getTraceAsString() . "\n";
}

echo "\n";