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

const timeout_npc = "raz:out"; // Entity identifier

// Timeout Confirmation Menu
function openTimeoutMenu(player) {
    const title = "§l§eTimeout Menu";
    const form = new ActionFormData()
        .title(title)
        .body("§lDo you want to time out?")
        .button("Yes", "textures/ui/confirm.png")
        .button("No", "textures/ui/cancel.png");

    form.show(player).then((r) => {
        if (r.selection === 0) executeTimeout(player);
    });
}

// Execute Timeout Actions
function executeTimeout(player) {
    player.runCommandAsync("setblock 9998 101 9980 redstone_block");
    player.runCommandAsync("tp @e[family=exit2] 9927 101 9952");
}

// Interaction Event for Timeout NPC
world.beforeEvents.playerInteractWithEntity.subscribe((data) => {
    const player = data.player;
    const targetEntity = data.target;

    if (targetEntity.typeId === timeout_npc) {
        system.run(() => openTimeoutMenu(player));
    }
});
