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

const map_shop = "jam:npc_clinic";

// Main Menu
function main(player, targetEntity) {
    const title = "§l§eField Clinic";
    const form = new ActionFormData()
        .title(title)
        .body(
            `§l§c🏥 Field Clinic Report\n\n§rYou've sustained injuries in the line of duty! Sit tight while our medics patch you up and your squad secures the area.\n\n§aTeam Status: All clear? You'll rejoin the action immediately once your teammate clear the floor!\n\n§cEmergency Protocol: If your crew falls, we'll perform a strategic withdrawal back to HQ.\n\n§eStay sharp, brave firefighter—your squad\ndepends on you!`
        )
        .button('Dismiss Clinic\n§7[Back on Duty]', '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));
    }
});
