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

// === Configurable block list for slicing ===
const SLICED_BLOCKS = [
    "aria_ah:skeleton",
    "aria_ah:corpse"
];

system.afterEvents.scriptEventReceive.subscribe(async (event) => {
    if (event.id !== "aria_ah:slice_blocks") return;
    
    const source = event.sourceEntity;
    if (!source || !source.isValid) return;

    const debugEnabled = isDebugEnabled();

    const startX = Math.floor(source.location.x);
    const startY = Math.floor(source.location.y) - 1;
    const startZ = Math.floor(source.location.z);
    const dimension = source.dimension;
    
    let destroyedCount = 0;

    // Cylinder search (radius 5, height 3)
    for (let x = -5; x <= 5; x++) {
        for (let z = -5; z <= 5; z++) {
            if (Math.hypot(x, z) > 5) continue;
            for (let yOffset = 0; yOffset < 3; yOffset++) {
                const bx = startX + x;
                const by = startY + yOffset;
                const bz = startZ + z;
                const block = dimension.getBlock({ x: bx, y: by, z: bz });
                if (block && SLICED_BLOCKS.includes(block.typeId)) {
                    // Use setblock ... destroy to drop loot
                    await dimension.runCommand(
                        `setblock ${bx} ${by} ${bz} air destroy`
                    );
                    destroyedCount++;
                }
            }
        }
    }

    if (debugEnabled) {
        world.sendMessage(`§aSlice destroyed ${destroyedCount} blocks (${SLICED_BLOCKS.join(", ")}) around ${startX},${startY},${startZ}`);
    }
});

// Reuse existing debug function from your main script
function isDebugEnabled() {
    try {
        const scoreboard = world.scoreboard.getObjective("aria_ah.dev_mode");
        return scoreboard?.getScore({ displayName: "debug" }) === 1;
    } catch (e) {
        return false;
    }
}
