// -------------------------------------------------------------
// EINMALIGES MIGRATIONSSKRIPT
// Überträgt tiles.json und objectConfig.json in die neuen
// "tile_config"- und "object_config"-Tabellen der Datenbank.
//
// Ausführen mit:  node migrate_configs.js
// (im selben Ordner wie server.js, NACHDEM die beiden Tabellen
//  per SQL angelegt wurden)
// -------------------------------------------------------------

import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import mysql from "mysql2/promise";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const db = await mysql.createPool({
    host: "156.67.28.205",
    user: "game",
    password: "tito13101",
    database: "gamegta",
    port: "3406",
    connectionLimit: 5
});

async function migrate() {
    // --- TILES ---
    const tilesPath = path.join(__dirname, "tiles.json");
    if (fs.existsSync(tilesPath)) {
        const tiles = JSON.parse(fs.readFileSync(tilesPath, "utf8"));
        let count = 0;

        for (const [id, tile] of Object.entries(tiles)) {
            await db.query(
                `INSERT INTO tile_config (id, name, color, collision) VALUES (?, ?, ?, ?)
                 ON DUPLICATE KEY UPDATE name=?, color=?, collision=?`,
                [
                    Number(id), tile.name || ("Tile " + id), tile.color || "#888888", tile.collision ? 1 : 0,
                    tile.name || ("Tile " + id), tile.color || "#888888", tile.collision ? 1 : 0
                ]
            );
            count++;
        }
        console.log(`✅ ${count} Tile(s) migriert.`);
    } else {
        console.log("Keine tiles.json gefunden - übersprungen.");
    }

    // --- OBJECTS ---
    const objectsPath = path.join(__dirname, "objectConfig.json");
    if (fs.existsSync(objectsPath)) {
        const objects = JSON.parse(fs.readFileSync(objectsPath, "utf8"));
        let count = 0;

        for (const [type, obj] of Object.entries(objects)) {
            await db.query(
                `INSERT INTO object_config (type, name, color, width, height, collision, interactive, action)
                 VALUES (?, ?, ?, ?, ?, ?, ?, ?)
                 ON DUPLICATE KEY UPDATE name=?, color=?, width=?, height=?, collision=?, interactive=?, action=?`,
                [
                    type, obj.name || type, obj.color || "#888888", obj.width || 32, obj.height || 32,
                    obj.collision ? 1 : 0, obj.interactive ? 1 : 0, obj.action || null,
                    obj.name || type, obj.color || "#888888", obj.width || 32, obj.height || 32,
                    obj.collision ? 1 : 0, obj.interactive ? 1 : 0, obj.action || null
                ]
            );
            count++;
        }
        console.log(`✅ ${count} Objekt-Typ(en) migriert.`);
    } else {
        console.log("Keine objectConfig.json gefunden - übersprungen.");
    }

    console.log("Migration abgeschlossen.");
    console.log("Du kannst tiles.json/objectConfig.json jetzt umbenennen/archivieren, sobald du geprüft hast, dass alles im Spiel korrekt lädt.");
    process.exit(0);
}

migrate().catch(err => {
    console.error("Migration fehlgeschlagen:", err);
    process.exit(1);
});
