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

const map_shop = "jam:npc_lobby";

// Main Menu
function main(player, targetEntity) {
    const title = "§l§eGuide Firefighter";
    const form = new ActionFormData()
        .title(title)
        .body(
            `§l§6🔥 Welcome Brave Firefighter! 🚒\n\n§fReady to become a hero? Choose your challenge:\n\n§a✦ Small Fire §f- Perfect for rookie\nfirefighters! Learn the basics of firefighting in a controlled environment.\n\n§6✦ Medium Blaze §f- For experienced\nfirefighters! Bigger fires, more challenges, and greater rewards await!\n\n§c✦ Inferno Mode §f- The ultimate test! Only\nthe bravest firefighters dare to face these massive infernos!\n\n§e💡 Remember: Every great firefighter started\nwith a small flame!`
        )
        .button('Exit\n§7[Back to Training]', 'textures/ui/crossout');

    form.show(player).then((r) => {
        // Exit is the only option, no handlers needed
    });
}

// Interaction Events
world.beforeEvents.playerInteractWithEntity.subscribe((data) => {
    const player = data.player;
    const targetEntity = data.target;

    if (targetEntity.typeId === map_shop) {
        system.run(() => main(player, targetEntity));
    }
});

world.afterEvents.entityHitEntity.subscribe((data) => {
    const player = data.damagingEntity;
    const targetEntity = data.hitEntity;

    if (player && targetEntity && targetEntity.typeId === map_shop) {
        system.run(() => main(player, targetEntity));
    }
});
