<?php
try {
    $dbPath = dirname(__DIR__) . '/ibo_panel.db';
    $db = new PDO("sqlite:$dbPath");
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // Lista de todas las tablas a crear
    $tablas = [

        "qr_code" => "
            CREATE TABLE IF NOT EXISTS qr_code (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                name TEXT NOT NULL,
                url TEXT NOT NULL
            )",

        "ibo" => "
            CREATE TABLE IF NOT EXISTS ibo (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                mac_address VARCHAR(100),
                username VARCHAR(100),
                password VARCHAR(100),
                expire_date VARCHAR(100),
                url VARCHAR(100),
                title VARCHAR(100),
                created_at VARCHAR(100),
                access_count INTEGER DEFAULT 0
            )",

        "adsstatus" => "
            CREATE TABLE IF NOT EXISTS adsstatus (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                adstype TEXT
            )",

        "ad_type" => "
            CREATE TABLE IF NOT EXISTS ad_type (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                ad_type TEXT NOT NULL
            )",

        "ads" => "
            CREATE TABLE IF NOT EXISTS ads (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                title VARCHAR(100),
                url TEXT
            )",

        "dns" => "
            CREATE TABLE IF NOT EXISTS dns (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                title TEXT NOT NULL,
                url TEXT NOT NULL
            )",

        "logintheme" => "
            CREATE TABLE IF NOT EXISTS logintheme (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                themelog TEXT NOT NULL
            )",

        "playlist" => "
            CREATE TABLE IF NOT EXISTS playlist (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                dns_id INTEGER,
                mac_address TEXT NOT NULL,
                username TEXT NOT NULL,
                password TEXT NOT NULL,
                pin TEXT NOT NULL
            )",

        "settings" => "
            CREATE TABLE IF NOT EXISTS settings (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                note_title TEXT NOT NULL,
                note_content TEXT NOT NULL
            )",

        "sports" => "
            CREATE TABLE IF NOT EXISTS sports (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                header_n TEXT,
                border_c TEXT,
                background_c TEXT,
                text_c TEXT,
                days TEXT,
                api TEXT
            )",

        "themes" => "
            CREATE TABLE IF NOT EXISTS themes (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                theme_id TEXT NOT NULL
            )",

        "users" => "
            CREATE TABLE IF NOT EXISTS users (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                username TEXT NOT NULL UNIQUE,
                password TEXT NOT NULL,
                admin BOOLEAN NOT NULL DEFAULT 0,
                dealer BOOLEAN NOT NULL DEFAULT 0,
                balance REAL NOT NULL DEFAULT 0
            )"
    ];

    // Ejecutar todas las consultas
    foreach ($tablas as $nombre => $sql) {
        $db->exec($sql);
        echo "✔ Tabla <strong>$nombre</strong> verificada o creada correctamente.<br>";
    }

    echo "<br><strong>✅ Todas las tablas están listas.</strong>";

} catch (PDOException $e) {
    die("❌ Error al crear las tablas: " . $e->getMessage());
}
 