import { world } from "@minecraft/server";
import { CLANS, BIJUUS, SUSANOOS, EXTRAS, MANGEKYOU_DEFS, RINNEGANS, CONSUMOS, CLANNAMEICON } from "./config.js";
import { getScore, setScore, getProp, setProp, getClan, getModeSlot } from "./system.js";

export function getCategorias(player) {
    const clanTag = CLANNAMEICON.find(c => player.hasTag(c));
    const clanName = clanTag ? clanTag.charAt(0).toUpperCase() + clanTag.slice(1) : "Diamond";
    const clanIcon = clanTag ? `textures/items/pamobile/nz_cla_${clanTag}` : "textures/items/diamond";

    return [
        { id: "clan", nome: "§f", icon: clanIcon, nomevisual: `§aModos ${clanName}` },
        { id: "mangekyou_list", nome: "§f", icon: "textures/items/pamobile/nz_mangekyou_itachi", nomevisual: "Mangekyou & Eternos" },
        { id: "bijuu", nome: "§f", icon: "textures/items/pamobile/nz_alma_kurama_yang", nomevisual: "Bijus" },
        { id: "susanoo", nome: "§f", icon: "textures/items/pamobile/nz_susanoo_sasuke", nomevisual: "Susanoo" },
        { id: "sennin", nome: "§f", icon: "textures/items/pamobile/nz_sennin_naruto", nomevisual: "Sennin" },
        { id: "portoes", nome: "§f", icon: "textures/items/pamobile/nz_portao8_shimon", nomevisual: "Portoes" },
        { id: "marcas", nome: "§f", icon: "textures/items/pamobile/nz_maldicao_sasuke3", nomevisual: "Marcas" },
        { id: "karmas", nome: "§f", icon: "textures/items/pamobile/nz_karma_boruto_v1_", nomevisual: "Karmas" },
        { id: "doujutsu", nome: "§f", icon: "textures/items/pamobile/nz_rinnegan", nomevisual: "Doujutsus" },
        { id: "extras", nome: "§f", icon: "textures/items/pamobile/nz_modo_jashin", nomevisual: "Outros" }
    ];
}

export function getModosPorCategoria(player, categoriaId) {
    const modosPadronizados = [];
    const playerClan = getClan(player);

    if (categoriaId === "mangekyou_list") {
        if (MANGEKYOU_DEFS && MANGEKYOU_DEFS.length > 0) {
            MANGEKYOU_DEFS.forEach(def => {
                modosPadronizados.push({
                    original: def,
                    id: def.id,
                    nome: def.nome,
                    icon: `textures/items/pamobile/nz_mangekyou_${def.key}`,
                    tp: def.tp,
                    tagDesbloqueio: `mangekyou_${def.key}_unlocked`,
                    isEterno: false
                });
            });
        }
        const eternos = [
            { key: "eterno_sasuke", id: "nz:mg_eterno_sasuke", nome: "Eterno Sasuke", req: ["mangekyou_sasuke_unlocked", "mangekyou_itachi_unlocked"] },
            { key: "eterno_madara", id: "nz:mg_eterno_madara", nome: "Eterno Madara", req: ["mangekyou_madara_unlocked", "mangekyou_izuna_unlocked"] },
            { key: "eterno_lp", id: "nz:mg_eterno_lp", nome: "Eterno LP", req: ["mangekyou_lp_unlocked", "mangekyou_kaiser_unlocked"] },
            { key: "eterno_kaiser", id: "nz:mg_eterno_kaiser", nome: "Eterno Kaiser", req: ["mangekyou_lp_unlocked", "mangekyou_kaiser_unlocked"] },
            { key: "mangekyou_duplo", id: "nz:mangekyou_duplo", nome: "Mangekyou Duplo", req: ["mangekyou_kakashi_unlocked", "mangekyou_obito_unlocked"] }
        ];
        eternos.forEach(et => {
            const nomeFinal = et.key.replace("eterno_", ""); 
            let iconPath = `textures/items/pamobile/nz_mg_eterno_${nomeFinal}`;
            if (et.key === "mangekyou_duplo") iconPath = "textures/items/pamobile/nz_mangekyou_duplo";
            modosPadronizados.push({
                original: et, id: et.id, nome: et.nome, icon: iconPath, tp: 0, tagDesbloqueio: `mangekyou_${et.key}_unlocked`, isEterno: true
            });
        });
        return modosPadronizados;
    }

    if (categoriaId === "clan") {
        if (playerClan && CLANS[playerClan]) {
            const modosDoClan = CLANS[playerClan].modos || [];
            modosDoClan.forEach((m, index) => {
                let tagLock = null;
                if (m.tp && m.tp > 0) {
                    tagLock = `unlocked_${m.id.replace("nz:", "")}`;
                }
                modosPadronizados.push({
                    original: m,
                    id: m.id,
                    nome: m.nome || m.name,
                    icon: m.icon,
                    tp: m.tp || 0,
                    tagDesbloqueio: tagLock,
                    lvlReq: m.lvl || 0
                });
            });
        }
    }
    else if (categoriaId === "bijuu") {
        Object.keys(BIJUUS).forEach(key => {
            const b = BIJUUS[key];
            if (player.hasTag(b.tag)) {
                b.modos.forEach(m => {
                    modosPadronizados.push({
                        original: m, id: m.id, nome: m.name, icon: m.icon, tp: m.tp || 0,
                        tagDesbloqueio: `bijuu_${m.id}_comprado`
                    });
                });
            }
        });
    }
    else if (categoriaId === "susanoo") {
    Object.keys(SUSANOOS).forEach(key => {
        const s = SUSANOOS[key];
        if (player.hasTag(s.tag)) {
            // Modo inicial (Costela/Base) agora usa baseTp e tag corretamente
            modosPadronizados.push({
                original: s,
                id: s.baseItem,
                nome: "Forma Costela",
                icon: s.baseIcon,
                tp: s.baseTp || 0,
                tagDesbloqueio: `susanoo_${key}_comprado`,
                lvlReq: 0
            });

            // Evoluções
            s.evolutions.forEach(e => {
                modosPadronizados.push({
                    original: e,
                    id: e.id,
                    nome: e.name,
                    icon: e.icon,
                    tp: e.tp,
                    tagDesbloqueio: `susanoo_${e.id}_comprado`
                });
            });
        }
    });
}
    else if (categoriaId === "sennin") {
        EXTRAS.sennins.forEach(s => { modosPadronizados.push({ ...s, tagDesbloqueio: `extras_${s.id}_comprado`, original: s }); });
    }
    else if (categoriaId === "portoes") {
        EXTRAS.portoes.forEach(p => { modosPadronizados.push({ ...p, tagDesbloqueio: `extras_${p.id}_comprado`, original: p }); });
    }
    else if (categoriaId === "marcas") {
        Object.keys(EXTRAS.marcas).forEach(marcaKey => {
            const listaMarcas = EXTRAS.marcas[marcaKey];
            const tagBase = listaMarcas[0].tag;
            if (tagBase && player.hasTag(tagBase)) {
                listaMarcas.forEach(m => { modosPadronizados.push({ ...m, tagDesbloqueio: `comprou_${m.id}`, original: m }); });
            }
        });
    }
    else if (categoriaId === "karmas") {
        Object.keys(EXTRAS.karmas).forEach(kKey => {
            const lista = EXTRAS.karmas[kKey];
            if (lista.length > 0 && player.hasTag(lista[0].tag)) {
                lista.forEach(k => { modosPadronizados.push({ ...k, tagDesbloqueio: `comprou_${k.id}`, original: k }); });
            }
        });
    }
    else if (categoriaId === "doujutsu") {
        RINNEGANS.forEach(r => { modosPadronizados.push({ original: r, id: r.id, nome: r.nome, icon: r.icon, tp: 0, tagDesbloqueio: null, isRinnegan: true }); });
    }
    else if (categoriaId === "extras") {
        if (EXTRAS.extras) {
            EXTRAS.extras.forEach(ex => {
                modosPadronizados.push({ 
                    ...ex, 
                    tagDesbloqueio: `extras_${ex.id}_comprado`, 
                    original: ex 
                });
            });
        }
    }

    return modosPadronizados;
}

function normalizarId(id) {
    return id.replace("nz:", "");
}

export function getEstadoModo(player, modo) {
    const idNorm = normalizarId(modo.id);
    const tagAtivo = `modo_${idNorm}_ativo`;
    if (player.hasTag(tagAtivo)) return 2;

    if (modo.tagDesbloqueio && player.hasTag(modo.tagDesbloqueio)) return 1;

    if (modo.isEterno) {
        const reqs = modo.original.req || [];
        const temTudo = reqs.every(r => player.hasTag(r));
        return temTudo ? 0 : -1;
    }

    if (modo.tagDesbloqueio && modo.tp && modo.tp > 0) return 0;

    if (modo.original && modo.original.tag && player.hasTag(modo.original.tag)) return 1;

    if (modo.lvlReq > 0) {
        const lvl = getScore(player, "Lvl");
        return lvl >= modo.lvlReq ? 1 : 0;
    }

    if (modo.isRinnegan) {
        const reqs = modo.original.req || [];
        return reqs.every(r => player.hasTag(r)) ? 1 : 0;
    }

    if (modo.tp && modo.tp > 0 && !modo.tagDesbloqueio) return 0;

    return 0;
}

export function executarAcaoModo(player, modo, estadoAtual) {
    const idNorm = normalizarId(modo.id);

    
    
    
    if (estadoAtual === 2) {
        const tagAtivo = `modo_${idNorm}_ativo`;
        player.removeTag(tagAtivo);
        
        const slot = getModeSlot(modo.id);
        
        player.runCommandAsync(`replaceitem entity @s ${slot} 0 air 1`);
        player.sendMessage(`§c${modo.nome} Desativado.`);

        
        const itemsToRemove = [
            "nz:kirin", 
            "nz:inseto_aburame", 
            "nz:bala_de_crystal", 
            "nz:dragao_de_crystal", 
            "nz:shuriken_papel", 
            "nz:bomba_c1", 
            "nz_sasuke:shield",
            "nz:marionete_kazekage", 
            "nz:aurela_de_papel", 
            "nz:argila", 
            "nz:bomba_c2_j", 
            "nz:crystal_sword", 
            "nz:braco_danzou_selado", 
            "nz:braco_danzou_liberado",
            "nz:8_trigramas_rotacao",
            "nz:8_trigramas_rotacao_reset",
            "nz:apagar_amaterasu",
            "nz:jutsu_amaterasu",
            "nz:kotoamatsukami",
            "nz:cano_bolha",
            "nz:mangekyou_genjutsu",
            "nz:sha_genjutsu",
            "nz:expansao_parcial1",
            "nz:expansao_parcial2",
            "nz:expansao_parcial3",
            "nz:remover_expansao1",
            "nz:shakujo_naruto",
            "nz:shakujo_obito",
            "nz:selar",
            "nz:kamui_",
            "minecraft:totem_of_undying",
            "nz:rinne_genjutsu",
            "nz:shinra_tensei",
            "nz:rasen_shuriken",
            "nz:shakujo_madara",
            "nz:bijuudama"
        ];

        
        
        itemsToRemove.forEach(itemId => {
            player.runCommandAsync(`clear @s ${itemId}`).catch(() => {});
        });

        return true;
    }

    
    
    
    if (estadoAtual === 1) {
        const slotTarget = getModeSlot(modo.id);
        player.addTag(`modo_${idNorm}_ativo`);
        
        
        player.runCommandAsync(`replaceitem entity @s ${slotTarget} 0 ${modo.id} 1 0 {"minecraft:item_lock":{"mode":"lock_in_slot"}}`);

        
        
        if (modo.id === "nz:maldicao_sasuke3") {
            player.runCommandAsync(`give @s nz:kirin 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        
        if (modo.id === "nz:expansao_lvl1") {
            player.runCommandAsync(`give @s nz:expansao_parcial1 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:remover_expansao1 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        } else if (modo.id === "nz:expansao_lvl2") {
            player.runCommandAsync(`give @s nz:expansao_parcial2 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:remover_expansao1 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        } else if (modo.id === "nz:expansao_lvl3") {
            player.runCommandAsync(`give @s nz:expansao_parcial3 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:remover_expansao1 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        else if (modo.id.includes("expansao_lvl")) {
             const lvl = modo.id.split("lvl")[1];
             player.runCommandAsync(`give @s nz:expansao_parcial${lvl} 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
             player.runCommandAsync(`give @s nz:remover_expansao1 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }

        if (modo.id === "nz:mangekyou_itachi" || modo.id === "nz:mangekyou_sasuke") {
            player.runCommandAsync(`give @s nz:apagar_amaterasu 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:jutsu_amaterasu 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:mg_eterno_sasuke") {
            player.runCommandAsync(`give @s nz:apagar_amaterasu 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:jutsu_amaterasu 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:mangekyou_genjutsu 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:mg_eterno_madara") {
            player.runCommandAsync(`give @s nz:mangekyou_genjutsu 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:mangekyou_shisui") {
            player.runCommandAsync(`give @s nz:kotoamatsukami 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:mangekyou_kakashi") {
            player.runCommandAsync(`give @s nz:kamui_ 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:mangekyou_obito") {
            player.runCommandAsync(`give @s nz:kamui_ 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:mangekyou_bolinho") {
            player.runCommandAsync(`give @s nz:kamui 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:jutsu_amaterasu 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:kotoamatsukami  1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:crystal_full_form") {
            player.runCommandAsync(`give @s nz:bala_de_crystal 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:dragao_de_crystal 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:crystal_sword  1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:crystal_half_form") {
            player.runCommandAsync(`give @s nz:bala_de_crystal 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:crystal_sword  1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:crystal_form") {
            player.runCommandAsync(`give @s nz:bala_de_crystal 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:konan_form") {
            player.runCommandAsync(`give @s nz:shuriken_papel 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:aurela_papel  1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:sasori_form") {
            player.runCommandAsync(`give @s nz:marionete_kazekage 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:deidara_form") {
            player.runCommandAsync(`give @s nz:bomba_c1 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:bomba_c2_j 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);

        }
        if (modo.id === "nz:mangekyou_jokker") {
            player.runCommandAsync(`give @s nz:kamui 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:jutsu_amaterasu 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:kotoamatsukami  1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:mangekyou_kaiser") {
            player.runCommandAsync(`give @s nz:kamui_ 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:jutsu_amaterasu 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:kotoamatsukami  1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:selar 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:mangekyou_lp") {
            player.runCommandAsync(`give @s nz:kamui_ 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:jutsu_amaterasu 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:kotoamatsukami  1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:selar 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:mg_eterno_kaiser") {
            player.runCommandAsync(`give @s nz:kamui_ 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:jutsu_amaterasu 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:kotoamatsukami  1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:bijuudama 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:selar 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:shinra_tensei 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:flor_de_fenix 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:kirin 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:mg_eterno_lp") {
            player.runCommandAsync(`give @s nz:kamui_ 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:jutsu_amaterasu 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:kotoamatsukami  1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:bijuudama 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:selar 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:shinra_tensei 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:flor_de_fenix 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:kirin 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:mangekyou_bruno") {
            player.runCommandAsync(`give @s nz:kamui_ 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:jutsu_amaterasu 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:kotoamatsukami  1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:bijuudama 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:selar 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:shinra_tensei 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:flor_de_fenix 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:kirin 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:modo_panda") {
            player.runCommandAsync(`give @s nz:kamui_ 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:jutsu_amaterasu 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:kotoamatsukami  1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:bijuudama 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:selar 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:shinra_tensei 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:flor_de_fenix 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:kirin 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:rinne_mangekyou_obito") {
            player.runCommandAsync(`give @s nz:kamui_ 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:mangekyou_duplo") {
            player.runCommandAsync(`give @s nz:kamui_ 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:mangekyou_genjutsu 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id.startsWith("nz:manto_saiken")) {
            player.runCommandAsync(`give @s nz:cano_bolha 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id.startsWith("nz:mangekyou")) {
            player.runCommandAsync(`give @s nz:mangekyou_genjutsu 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id.startsWith("nz:rinne")) {
            player.runCommandAsync(`give @s nz:selar 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:rinne_genjutsu 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
            player.runCommandAsync(`give @s nz:shinra_tensei 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id.startsWith("nz:modo_bijuu")) {
            player.runCommandAsync(`give @s nz:bijuudama 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id.startsWith("nz:manto_juubi1")) {
            player.runCommandAsync(`give @s nz:bijuudama 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id.startsWith("nz:manto_juubi8")) {
            player.runCommandAsync(`give @s nz:bijuudama 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id.startsWith("nz:manto_juubi9")) {
            player.runCommandAsync(`give @s nz:bijuudama 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id.startsWith("nz:susanoo_sasuke")) {
            player.runCommandAsync(`give @s nz_sasuke:shield 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:sha3_obito" || modo.id === "nz:sha3_kakashi") {
            player.runCommandAsync(`give @s nz:sha_genjutsu 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:sharingan3") {
            player.runCommandAsync(`give @s nz:sha_genjutsu 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:ketsuryugan") {
            player.runCommandAsync(`give @s nz:sha_genjutsu 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:ketsuryugan_8dragoes") {
            player.runCommandAsync(`give @s nz:sha_genjutsu 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:manto_kuramarikudou") {
            player.runCommandAsync(`give @s nz:shakujo_naruto 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:manto_matatabi2") {
            player.runCommandAsync(`give @s nz:bijuudama 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:manto_isobu3") {
            player.runCommandAsync(`give @s nz:bijuudama 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:manto_songoku4") {
            player.runCommandAsync(`give @s nz:bijuudama 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:manto_kokuo5") {
            player.runCommandAsync(`give @s nz:bijuudama 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:manto_saiken6") {
            player.runCommandAsync(`give @s nz:bijuudama 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:manto_chomei5") {
            player.runCommandAsync(`give @s nz:bijuudama 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:manto_gyuki7") {
            player.runCommandAsync(`give @s nz:bijuudama 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:manto_kurama4") {
            player.runCommandAsync(`give @s nz:bijuudama 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:manto_juubiobito") {
            player.runCommandAsync(`give @s nz:shakujo_obito 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:manto_juubimadara") {
            player.runCommandAsync(`give @s nz:shakujo_madara 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:braco_danzou_selado") {
             player.runCommandAsync(`give @s minecraft:totem_of_undying 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:braco_danzou_liberado") {
             player.runCommandAsync(`give @s minecraft:totem_of_undying 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
             player.runCommandAsync(`give @s nz:kotoamatsukami  1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id === "nz:sennin_naruto") {
             player.runCommandAsync(`give @s nz:rasen_shuriken 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }
        if (modo.id.startsWith("nz:insetos_aburame")) {
             player.runCommandAsync(`give @s nz:inseto_aburame 1 0 {"minecraft:item_lock":{"mode":"lock_in_inventory"}}`);
        }

        player.sendMessage(`§a${modo.nome} Ativado!`);
        return true;
    }

    
    
    
    if (estadoAtual === 0) {
        if (modo.isEterno) {
            if (modo.tagDesbloqueio) player.addTag(modo.tagDesbloqueio);
            player.sendMessage(`§a${modo.nome} Despertado!`);
            player.playSound("random.levelup");
            return true;
        }

        if (modo.tp > 0) {
            const tp = getScore(player, "TP");
            if (modo.original && modo.original.lvl) {
                const lvl = getScore(player, "Lvl");
                if (lvl < modo.original.lvl) {
                    player.sendMessage(`§cVocê precisa ter nível ${modo.original.lvl} para comprar este modo.`);
                    return false;
                }
            }

            
            if (modo.original && modo.original.tag && !player.hasTag(modo.original.tag)) {
                player.sendMessage("§cVocê não possui a tag necessária para este modo.");
                return false;
            }

            if (tp >= modo.tp) {
                setScore(player, "TP", tp - modo.tp);
                if (modo.tagDesbloqueio) player.addTag(modo.tagDesbloqueio);

                if (modo.id.includes("mangekyou") && modo.original && modo.original.key) {
                     const susanooKey = modo.original.key;
                     if(SUSANOOS[susanooKey]) {
                        player.addTag(`susanoo_${susanooKey}_unlocked`);
                     }
                }

                player.sendMessage(`§aComprado: ${modo.nome} (-${modo.tp} TP)`);
                player.playSound("random.levelup");
                return true;
            } else {
                player.sendMessage(`§cTP Insuficiente (${modo.tp})`);
                return false;
            }
        }
    }

    
    
    
    if (estadoAtual === -1 && modo.isEterno) {
        player.sendMessage(`§cFaltam requisitos para ${modo.nome}.`);
        return false;
    }

    return false;
}