import { world, system } from "@minecraft/server";

// ====== ENTIDADES VANILLA ======
const hostileEntities = [
    "minecraft:zombie","minecraft:creeper","minecraft:skeleton","minecraft:spider",
    "minecraft:enderman","minecraft:witch","minecraft:blaze","minecraft:stray",
    "minecraft:husk","minecraft:drowned","minecraft:phantom","minecraft:slime",
    "minecraft:magma_cube","minecraft:pillager","minecraft:vindicator","minecraft:evoker",
    "minecraft:guardian","minecraft:elder_guardian","minecraft:wither_skeleton",
    "minecraft:wither","minecraft:warden"
];
const passiveEntities = [
    "minecraft:pig","minecraft:donkey","minecraft:horse","minecraft:parrot",
    "minecraft:bat","minecraft:cat","minecraft:wolf","minecraft:chicken","minecraft:fox"
];

// ====== ITENS VANILLA ======
const itemTypes = [
    "minecraft:diamond_sword","minecraft:netherite_sword","minecraft:bow","minecraft:crossbow",
    "minecraft:trident","minecraft:shield","minecraft:diamond_pickaxe","minecraft:netherite_pickaxe",
    "minecraft:diamond_axe","minecraft:netherite_axe","minecraft:diamond_helmet","minecraft:diamond_chestplate",
    "minecraft:diamond_leggings","minecraft:diamond_boots","minecraft:netherite_helmet","minecraft:netherite_chestplate",
    "minecraft:netherite_leggings","minecraft:netherite_boots","minecraft:coal","minecraft:iron_ingot",
    "minecraft:gold_ingot","minecraft:diamond","minecraft:emerald","minecraft:netherite_ingot",
    "minecraft:apple","minecraft:golden_apple","minecraft:bread","minecraft:cooked_beef",
    "minecraft:cooked_chicken","minecraft:cooked_porkchop","minecraft:carrot","minecraft:potato",
    "minecraft:baked_potato","minecraft:cookie","minecraft:melon_slice","minecraft:pumpkin_pie"
];

// ====== COMANDOS ALEATÓRIOS ======
const randomCommands = [
    "kill @e[type=!player]","time set day","time set night",
    "weather clear","weather rain","weather thunder","summon tnt"
];

// ====== ITENS ADDON (todos IDs de CONSUMOS) ======
const addonItems = [
    "nz:expansao_lvl1","nz:expansao_lvl2","nz:expansao_lvl3","npc_aburame_proximo","nz:olho_senju","nz:sharingan",
    "nz:sharingan2","nz:senju_ira","nz:senju_sennin","nz:senju_golem","nz:senju_1k","nz:sha3_obito","nz:sha3_kakashi",
    "nz:sharingan3","nz:konan_form","nz:kakuzo_form","nz:kakuzo2_form","nz:deidara_form","nz:sasori_form","nz:sasori2",
    "nz:olho_uzumaki","nz:uzumaki_beserker","nz:corrente_beserker","nz:inseto_aburame_m","nz:inseto_aburame_half",
    "nz:inseto_aburame_half_poison","nz:inseto_aburame_full","nz:inseto_aburame_full_poison","nz:corrente_uzumaki",
    "nz:inseto_aburame_m2","nz:byakugan","nz:bk_ativado","nz:byakugan_otsutsuki","nz:bk_otsutsuki_ativado",
    "nz:tenseigan_olho","nz:tenseigan_manto","nz:jougan","nz:karma_boruto_v1","nz:karma_kawaki_v1","nz:ketsuryugan",
    "nz:ketsuryugan_8dragoes","nz:ketsuryugan_halfblood","nz:ketsuryugan_fullblood","nz:ketsuryugan_bloodmoon",
    "nz:crystal_form","nz:crystal_half_form","nz:crystal_full_form","nz:mangekyou_izuna","nz:mangekyou_rai",
    "nz:mangekyou_naori","nz:mangekyou_naka","nz:mangekyou_shin","nz:mangekyou_fugaku","nz:mangekyou_sarada",
    "nz:mangekyou_baru","nz:mangekyou_sasuke","nz:mangekyou_itachi","nz:mangekyou_madara","nz:mangekyou_shisui",
    "nz:mangekyou_kakashi","nz:mangekyou_obito","nz:mangekyou_lp","nz:mangekyou_kaiser","nz:mg_eterno_sasuke",
    "nz:mg_eterno_madara","nz:mangekyou_duplo","nz:mg_eterno_lp","nz:mangekyou_bruno","nz:mg_eterno_kaiser",
    "nz:modo_jashin","nz:susanoo_itachi","nz:susanoo_itachi2","nz:susanoo_itachi3","nz:susanoo_itachi4","nz:susanoo_itachi5",
    "nz:susanoo_sasuke","nz:susanoo_sasuke2","nz:susanoo_sasuke3","nz:susanoo_sasuke4","nz:susanoo_sasuke5",
    "nz:susanoo_sarada","nz:susanoo_sarada2","nz:susanoo_sarada3","nz:susanoo_sarada4","nz:susanoo_sarada5",
    "nz:susanoo_madara","nz:susanoo_madara2","nz:susanoo_madara3","nz:susanoo_madara4","nz:susanoo_madara5",
    "nz:susanoo_shisui","nz:susanoo_shisui2","nz:susanoo_shisui3","nz:susanoo_shisui4","nz:susanoo_shisui5",
    "nz:susanoo_kakashi","nz:susanoo_kakashi2","nz:susanoo_kakashi3","nz:susanoo_kakashi4","nz:susanoo_kakashi5",
    "nz:sennin_naruto","nz:sennin_jiraya","nz:sennin_hashirama","nz:sennin_kabuto","nz:sennin_orochimaru","nz:sennin_mitsuki",
    "nz:byakugou","nz:maldicao_tayuya1","nz:maldicao_tayuya2","nz:maldicao_tayuya3","nz:maldicao_kindomaru1",
    "nz:maldicao_kindomaru2","nz:maldicao_kindomaro3","nz:maldicao_kimimaro1","nz:maldicao_kimimaro2_v1","nz:maldicao_kimimaro2_v2",
    "nz:maldicao_kimimaro3","nz:maldicao_sasuke1","nz:maldicao_sasuke2","nz:maldicao_sasuke3","nz:maldicao_jirobo1",
    "nz:maldicao_jirobo2","nz:maldicao_jirobo3","nz:maldicao_sakon_ukon1","nz:maldicao_sakon_ukon2","nz:maldicao_sakon_ukon3",
    "nz:karma_boruto_v1_","nz:karma_boruto_v2_","nz:karma_kawaki_v1_","nz:karma_kawaki_v2_","nz:karma_kawaki_v2_sukuna",
    "nz:momoshiki_form","nz:karma_art","nz:urashiki_form","nz:kinshiki_form","nz:isshiki_form","nz:modo_samehada",
    "nz:jutsu_modo_trovao","nz:braco_danzou_selado","nz:braco_danzou_liberado","nz:manto_shukaku1","nz:manto_shukaku1_v",
    "nz:manto_shukakumk1","nz:manto_shukakumk2","nz:manto_shukakurikudou","nz:modo_barion_shukaku","nz:modo_bijuu_shukaku",
    "nz:manto_matatabi","nz:manto_matatabi2","nz:manto_matatabimk1","nz:manto_matatabimk2","nz:manto_matatabirikudou",
    "nz:modo_barion_matatabi","nz:modo_bijuu_matatabi","nz:manto_isobu1","nz:manto_isobu2","nz:manto_isobu3","nz:manto_isobumk1",
    "nz:manto_isobumk2","nz:manto_isoburikudou", "nz:modo_barion_isobu","nz:modo_bijuu_isobu",
    "nz:manto_songoku","nz:manto_songoku2","nz:manto_songoku4","nz:manto_songokumk1","nz:manto_songokumk2",
    "nz:manto_songokurikudou","nz:modo_barion_songoku","nz:modo_bijuu_songoku",
    "nz:manto_kokuo1","nz:manto_kokuo4","nz:manto_kokuo5","nz:modo_panda","nz:manto_kokuomk1","nz:manto_kokuomk2",
    "nz:manto_kokuorikudou","nz:modo_barion_kokuo","nz:modo_bijuu_kokuo",
    "nz:manto_saiken1","nz:manto_saiken3","nz:manto_saiken6","nz:manto_saiken6_escorregadio","nz:manto_saikenmk1",
    "nz:manto_saikenmk2","nz:manto_saikenrikudou","nz:modo_barion_saiken","nz:modo_bijuu_saiken",
    "nz:manto_chomei1","nz:manto_chomei3","nz:manto_chomei5","nz:manto_chomemk1","nz:manto_chomeimk2",
    "nz:manto_chomeirikudou","nz:modo_barion_chomei","nz:modo_bijuu_chomei",
    "nz:manto_gyuki1","nz:manto_gyuki6","nz:manto_gyuki7","nz:manto_gyukimk1","nz:manto_gyukimk2",
    "nz:manto_gyukirikudou","nz:modo_barion_gyuki","nz:modo_bijuu_gyuki",
    "nz:manto_kurama1","nz:manto_kurama2","nz:manto_kurama3","nz:manto_kurama4","nz:manto_kurama6","nz:manto_kurama8",
    "nz:manto_kuramamk1","nz:manto_kuramamk2","nz:manto_kuramarikudou","nz:modo_barion_kurama","nz:modo_bijuu_kurama",
    "nz:modo_bijuu_kurama_ashura",
    "nz:manto_juubi1","nz:manto_juubi8","nz:manto_juubi9","nz:manto_juubiobito","nz:manto_juubimadara",
    "nz:modo_barion_juubi",
    "nz:rinnegan","nz:rinne_mangekyou_obito","nz:rinnegan_supremo_sasuke"
];

// ====== MOBS ADDON ======
const addonEntities = [
    "nz:kisame_boss","nz:kisame_boss_v2","nz:madara_mangekyou","nz:juubi_v1","nz:juubi_v2",
    "nz:gyuki","nz:chomei","nz:saiken","nz:kokuo","nz:shukaku","nz:son_goku","nz:zabuza_boss",
    "nz:sasori","nz:sasori2","nz:konan","nz:akatsuki1","nz:akatsuki2"
];

// ====== ESTRUTURAS ======
const structures = ["missao7","kamui","hakuability","missao12","missao10","missao14","missao15"];

// ====== NOMES DE EVENTO ======
function getEventName(id) {
    if (id.includes("mangekyou") || id.includes("sharingan") || id.includes("rinnegan") || id.includes("jougan") || id.includes("byakugan") || id.includes("tenseigan")) {
        return "§dOlhos Proibidos";
    } else if (id.includes("susanoo")) {
        return "§5Manifestação do Susanoo";
    } else if (id.includes("senju") || id.includes("uzumaki") || id.includes("olho_senju") || id.includes("olho_uzumaki")) {
        return "§6Legado dos Clãs";
    } else if (id.includes("karma") || id.includes("momoshiki") || id.includes("kinshiki") || id.includes("isshiki") || id.includes("urashiki")) {
        return "§9Herança Otsutsuki";
    } else if (id.includes("barion") || id.includes("bijuu") || id.includes("manto")) {
        return "§4Fúria dos Bijuu";
    } else if (id.includes("expansao") || id.includes("corrente") || id.includes("inseto_aburame")) {
        return "§2Artes Proibidas";
    } else if (id.includes("sennin") || id.includes("byakugou")) {
        return "§aModo Sábio";
    } else if (id.includes("maldicao")) {
        return "§8Maldição Oculta";
    } else if (id.includes("jashin")) {
        return "§cRitual Jashinista";
    } else if (id.includes("samehada")) {
        return "§1Lâmina Voraz";
    } else if (id.includes("trovao")) {
        return "§bTempestade Shinobi";
    } else if (id.includes("danzou")) {
        return "§7Segredo de Danzou";
    } else if (id.includes("crystal")) {
        return "§3Cristal Proibido";
    }
    return "§6Artefato Shinobi";
}

// ====== FUNÇÃO DE AÇÃO ======
function getRandomAction(player) {
    const actions = [
        () => { // Item vanilla
            const item = itemTypes[Math.floor(Math.random() * itemTypes.length)];
            const amount = Math.floor(Math.random() * 15) + 1;
            player.runCommandAsync(`give @s ${item} ${amount}`);
            player.runCommandAsync(`titleraw @s title {"rawtext":[{"text":"§aTesouro Misterioso"}]}`);
        },
        () => { // Item addon
            const item = addonItems[Math.floor(Math.random() * addonItems.length)];
            const amount = Math.floor(Math.random() * 15) + 1;
            player.runCommandAsync(`give @s ${item} ${amount}`);
            player.runCommandAsync(`titleraw @s title {"rawtext":[{"text":"${getEventName(item)}"}]}`);
        },
        () => { // Mobs vanilla
            const mob = hostileEntities.concat(passiveEntities)[Math.floor(Math.random() * (hostileEntities.length+passiveEntities.length))];
            const amount = Math.floor(Math.random() * 6) + 5; // 5–10
            for (let i = 0; i < amount; i++) player.runCommandAsync(`summon ${mob}`);
            player.runCommandAsync(`titleraw @s title {"rawtext":[{"text":"§eInvasão"}]}`);
        },
        () => { // Mobs addon
            const mob = addonEntities[Math.floor(Math.random() * addonEntities.length)];
            const amount = Math.floor(Math.random() * 6) + 5; // 5–10
            for (let i = 0; i < amount; i++) player.runCommandAsync(`summon ${mob}`);
            player.runCommandAsync(`titleraw @s title {"rawtext":[{"text":"${getEventName(mob)}"}]}`);
        },
        () => { // Comando aleatório
            const cmd = randomCommands[Math.floor(Math.random() * randomCommands.length)];
            player.runCommandAsync(cmd);
            if (cmd.startsWith("kill")) player.runCommandAsync(`titleraw @s title {"rawtext":[{"text":"§4Juízo Final"}]}`);
            else if (cmd.startsWith("time")) player.runCommandAsync(`titleraw @s title {"rawtext":[{"text":"§eCiclo Celestial"}]}`);
            else if (cmd.startsWith("weather")) player.runCommandAsync(`titleraw @s title {"rawtext":[{"text":"§bCapricho dos Céus"}]}`);
            else if (cmd.includes("tnt")) player.runCommandAsync(`titleraw @s title {"rawtext":[{"text":"§cFúria Explosiva"}]}`);
            else player.runCommandAsync(`titleraw @s title {"rawtext":[{"text":"§6Evento Aleatório"}]}`);
        },
        () => { // Estrutura
            const struct = structures[Math.floor(Math.random() * structures.length)];
            player.runCommandAsync(`structure load ${struct} ~ ~ ~`);
            player.runCommandAsync(`titleraw @s title {"rawtext":[{"text":"§dEstrutura Invocada"}]}`);
        }
    ];

    actions[Math.floor(Math.random() * actions.length)]();
}

// ====== CONTAGEM REGRESSIVA ======
function countdown(player, seconds, callback) {
    let timeLeft = seconds;
    const interval = system.runInterval(() => {
        player.runCommandAsync(`titleraw @s title {"rawtext":[{"text":"§eEvento em ${timeLeft}s"}]}`);
        timeLeft--;
        if (timeLeft < 0) {
            system.clearRun(interval);
            callback();
        }
    }, 20); // 20 ticks =