<?php
require_once 'db/config.php';
$db = db();

$sqls = [
    "CREATE TABLE IF NOT EXISTS lootboxes (
        id INT AUTO_INCREMENT PRIMARY KEY,
        name VARCHAR(255) NOT NULL,
        slug VARCHAR(255) NOT NULL UNIQUE,
        description TEXT,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",

    "CREATE TABLE IF NOT EXISTS lootbox_rolls (
        id INT AUTO_INCREMENT PRIMARY KEY,
        lootbox_id INT NOT NULL,
        roll_count INT NOT NULL DEFAULT 1,
        probability DECIMAL(5,2) NOT NULL DEFAULT 100.00,
        FOREIGN KEY (lootbox_id) REFERENCES lootboxes(id) ON DELETE CASCADE
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",

    "CREATE TABLE IF NOT EXISTS lootbox_items (
        id INT AUTO_INCREMENT PRIMARY KEY,
        lootbox_id INT NOT NULL,
        resource_slug VARCHAR(255) NULL,
        probability DECIMAL(5,2) NOT NULL DEFAULT 0.00,
        quantity_min INT NOT NULL DEFAULT 1,
        quantity_max INT NOT NULL DEFAULT 1,
        FOREIGN KEY (lootbox_id) REFERENCES lootboxes(id) ON DELETE CASCADE,
        FOREIGN KEY (resource_slug) REFERENCES game_resources(slug) ON UPDATE CASCADE ON DELETE SET NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"
];

foreach ($sqls as $sql) {
    try {
        $db->exec($sql);
        echo "Executed: " . substr($sql, 0, 50) . "...\n";
    } catch (PDOException $e) {
        echo "Error: " . $e->getMessage() . "\n";
    }
}

