<?php
// Script pour créer une table spécifique
require_once __DIR__ . '/../app/helpers.php';

spl_autoload_register(function ($class) {
    $prefix = 'App\\';
    $baseDir = __DIR__ . '/../app/';
    if (strncmp($prefix, $class, strlen($prefix)) !== 0) return;
    $relative = substr($class, strlen($prefix));
    $file = $baseDir . str_replace('\\', '/', $relative) . '.php';
    if (file_exists($file)) require $file;
});

use App\Core\Database;

try {
    $pdo = Database::pdo();
    echo "✅ Connexion à la base de données réussie\n";
    
    // Créer la table liaisons
    $sql = "CREATE TABLE IF NOT EXISTS `liaisons` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(255) NOT NULL,
      `description` text DEFAULT NULL,
      `active` tinyint(1) DEFAULT 1,
      `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
      `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      PRIMARY KEY (`id`),
      UNIQUE KEY `name` (`name`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
    
    $pdo->exec($sql);
    echo "✅ Table liaisons créée\n";
    
    // Créer la table sites
    $sql = "CREATE TABLE IF NOT EXISTS `sites` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(255) NOT NULL,
      `description` text DEFAULT NULL,
      `active` tinyint(1) DEFAULT 1,
      `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
      `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      PRIMARY KEY (`id`),
      UNIQUE KEY `name` (`name`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
    
    $pdo->exec($sql);
    echo "✅ Table sites créée\n";
    
    // Créer la table priority_levels
    $sql = "CREATE TABLE IF NOT EXISTS `priority_levels` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(100) NOT NULL,
      `sort_order` int(11) DEFAULT 10,
      `color` varchar(7) DEFAULT '#6c757d',
      `description` text DEFAULT NULL,
      `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
      `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      PRIMARY KEY (`id`),
      UNIQUE KEY `name` (`name`),
      KEY `sort_order` (`sort_order`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
    
    $pdo->exec($sql);
    echo "✅ Table priority_levels créée\n";
    
    // Créer la table equipments
    $sql = "CREATE TABLE IF NOT EXISTS `equipments` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(255) NOT NULL,
      `category` varchar(100) DEFAULT NULL,
      `description` text DEFAULT NULL,
      `active` tinyint(1) DEFAULT 1,
      `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
      `updated_at` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      PRIMARY KEY (`id`),
      KEY `category` (`category`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
    
    $pdo->exec($sql);
    echo "✅ Table equipments créée\n";
    
    // Insérer des données d'exemple
    $inserts = [
        "INSERT IGNORE INTO `priority_levels` (`name`, `sort_order`, `color`, `description`) VALUES
        ('Critique', 1, '#dc3545', 'Incident critique nécessitant une intervention immédiate'),
        ('Haute', 2, '#fd7e14', 'Incident à haute priorité'),
        ('Normale', 3, '#ffc107', 'Incident à priorité normale'),
        ('Basse', 4, '#28a745', 'Incident à basse priorité');",
        
        "INSERT IGNORE INTO `equipments` (`name`, `category`, `description`, `active`) VALUES
        ('Serveur principal', 'Serveur', 'Serveur de production principal', 1),
        ('Switch réseau', 'Réseau', 'Commutateur réseau principal', 1),
        ('Routeur', 'Réseau', 'Routeur de connexion internet', 1),
        ('Firewall', 'Sécurité', 'Pare-feu de sécurité', 1),
        ('Fibre optique', 'Fibre', 'Infrastructure fibre optique', 1),
        ('UPS', 'Infrastructure', 'Système d\\'alimentation sans interruption', 1);",
        
        "INSERT IGNORE INTO `liaisons` (`name`, `description`, `active`) VALUES
        ('Liaison principale', 'Liaison fibre principale', 1),
        ('Liaison de secours', 'Liaison de backup', 1),
        ('Liaison interne', 'Liaison réseau interne', 1);",
        
        "INSERT IGNORE INTO `sites` (`name`, `description`, `active`) VALUES
        ('Abidjan Centre', 'Site principal d\\'Abidjan', 1),
        ('Bouaké', 'Site de Bouaké', 1),
        ('San Pedro', 'Site de San Pedro', 1),
        ('Yamoussoukro', 'Site de Yamoussoukro', 1);"
    ];
    
    foreach ($inserts as $insert) {
        try {
            $pdo->exec($insert);
            echo "✅ Données insérées\n";
        } catch (Exception $e) {
            echo "⚠️ Insertion : " . $e->getMessage() . "\n";
        }
    }
    
    echo "\n🎉 Toutes les tables sont maintenant créées !\n";
    
} catch (Exception $e) {
    echo "❌ Erreur : " . $e->getMessage() . "\n";
    exit(1);
}
?>