// Cache Text components at script load instead of creating per-spawn
const VARIANT_NAMES = {
    berserker: Text.yellow("Berserker Zombie"),
    bloated: Text.darkGreen("Bloated Zombie"),
    stalker: Text.darkGray("Stalker Zombie"),
    sentinel: Text.gray("Sentinel Zombie"),
    lancer: Text.yellow("Lancer Zombie")
}

EntityEvents.spawned('minecraft:zombie', event => {
    const { entity, level } = event

    // Performance check: Prevent re-processing and restrict to Overworld
    if (level.dimension !== 'minecraft:overworld' || entity.tags.contains('pota_variant_processed')) return
    entity.tags.add('pota_variant_processed')

    // Selection logic
    let roll = Math.random() * 100
    let cumulativeChance = 0

    // HELPER FUNCTION: Apply Gear & Attributes
    const applyVariant = (name, health, speed, gear) => {
        if (name) {
            entity.setCustomName(name)
            entity.customNameVisible = true
        }
        
        if (health) {
            entity.setAttributeBaseValue('minecraft:generic.max_health', health)
            entity.health = health
        }
        
        if (speed) entity.setAttributeBaseValue('minecraft:generic.movement_speed', speed)
        
        if (gear) {
            if (gear.head) entity.headArmorItem = gear.head
            if (gear.chest) entity.chestArmorItem = gear.chest
            if (gear.legs) entity.legsArmorItem = gear.legs
            if (gear.feet) entity.feetArmorItem = gear.feet
            if (gear.main) entity.mainHandItem = gear.main
            if (gear.off) entity.offHandItem = gear.off

            // Prevent gear drops
            entity.setDropChance('head', 0)
            entity.setDropChance('chest', 0)
            entity.setDropChance('legs', 0)
            entity.setDropChance('feet', 0)
            entity.setDropChance('mainhand', 0)
            entity.setDropChance('offhand', 0)
        }
    }

    // --- 1. BERSERKER (5%) ---
    cumulativeChance += 5
    if (roll <= cumulativeChance) {
        applyVariant(VARIANT_NAMES.berserker, 25, 0.25, {
            head: "minecraft:golden_helmet",
            chest: "minecraft:golden_chestplate",
            legs: "minecraft:golden_leggings",
            feet: "minecraft:golden_boots",
            main: "berserker_rpg:iron_berserker_axe",
            off: "berserker_rpg:iron_berserker_axe"
        })
        entity.setAttributeBaseValue('minecraft:generic.knockback_resistance', 0.5)
        return
    }

    // --- 2. BLOATED (5%) ---
    cumulativeChance += 5
    if (roll <= cumulativeChance) {
        applyVariant(VARIANT_NAMES.bloated, 40, 0.15, null)
        entity.setAttributeBaseValue('minecraft:generic.knockback_resistance', 1.0)
        return
    }

    // --- 3. STALKER (10%) ---
    cumulativeChance += 10
    if (roll <= cumulativeChance) {
        applyVariant(VARIANT_NAMES.stalker, 18, 0.30, {
            head: "minecraft:chainmail_helmet",
            chest: "minecraft:chainmail_chestplate",
            legs: "minecraft:chainmail_leggings",
            feet: "minecraft:chainmail_boots",
            main: "rogues:iron_dagger",
            off: "rogues:iron_dagger"
        })
        entity.setAttributeBaseValue('minecraft:generic.follow_range', 40)
        return
    }

    // --- 4. SENTINEL (10%) ---
    cumulativeChance += 10
    if (roll <= cumulativeChance) {
        applyVariant(VARIANT_NAMES.sentinel, 30, 0.20, {
            head: "minecraft:iron_helmet",
            chest: "minecraft:iron_chestplate",
            legs: "minecraft:iron_leggings",
            feet: "minecraft:iron_boots",
            main: "paladins:iron_mace",
            off: "paladins:iron_kite_shield"
        })
        return
    }

    // --- 5. LANCER (15%) ---
    cumulativeChance += 15
    if (roll <= cumulativeChance) {
        applyVariant(VARIANT_NAMES.lancer, null, null, {
            head: "minecraft:leather_helmet",
            chest: "minecraft:leather_chestplate",
            legs: "minecraft:leather_leggings",
            feet: "minecraft:leather_boots",
            main: "archers:iron_spear"
        })
        return
    }
})