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

const CHAKRA_CONSUMPTION_AMOUNT = 5;
const CONSUMPTION_INTERVAL_TICKS = 100; // 5 segundos
const ACTIVATION_TAG = "consumir_chakra";

system.runInterval(async () => {
  for (const player of world.getPlayers()) {
    if (player.hasTag(ACTIVATION_TAG)) {
      try {
        await player.runCommandAsync(`scoreboard players remove @s chakra ${CHAKRA_CONSUMPTION_AMOUNT}`);
        const testResult = await player.runCommandAsync(`scoreboard players test @s chakra 1 *`);

        if (testResult.successCount === 0) {
          await player.runCommandAsync(`tag @s remove ${ACTIVATION_TAG}`);
          await player.runCommandAsync(`titleraw @s actionbar {"rawtext":[{"text":"§cSeu chakra acabou! Modo desativado."}]}`);

          // Remove capacete e efeitos
          await player.runCommandAsync(`replaceitem entity @s slot.armor.head 1 air`);
          await player.runCommandAsync(`tag @s remove ver_chakra`); // ✅ Correção aqui
          await player.runCommandAsync(`effect @s clear`);
          await player.runCommandAsync(`tag @s remove cabelo0`);
          await player.runCommandAsync(`clear @s spk:jutsu_rinnegan`);
        }
      } catch (error) {
        console.error(`[CHAKRA_CONSUMER] Erro no player ${player.name}: ${error}`);
      }
    }
  }
}, CONSUMPTION_INTERVAL_TICKS);
