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

    // 1. PERFORMANCE CHECK: Prevent re-processing and restrict to Overworld
    // This tag ensures we only run this logic ONCE per zombie lifetime.
    if (level.dimension !== 'minecraft:overworld' || entity.tags.contains('pota_variant_processed')) return
    entity.tags.add('pota_variant_processed')

    // 2. SELECTION LOGIC
    let roll = Math.random() * 100
    let cumulativeChance = 0

    // HELPER FUNCTION: Apply Gear & Attributes
    const applyVariant = (name, health, speed, gear) => {
        if (name) entity.customName = name
        
        if (health) {
            // Correct way to modify health to avoid attribute sync issues
            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 variants from dropping their "special" gear
            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(Text.yellow("Berserker Zombie"), 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(Text.darkGreen("Bloated Zombie"), 40, 0.15, null)
        entity.setAttributeBaseValue('minecraft:generic.knockback_resistance', 1.0)
        return
    }

    // --- 3. STALKER (10%) ---
    cumulativeChance += 10
    if (roll <= cumulativeChance) {
        applyVariant(Text.darkGray("Stalker Zombie"), 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(Text.gray("Sentinel Zombie"), 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(Text.yellow("Lancer Zombie"), null, null, {
            head: "minecraft:leather_helmet",
            chest: "minecraft:leather_chestplate",
            legs: "minecraft:leather_leggings",
            feet: "minecraft:leather_boots",
            main: "archers:iron_spear"
        })
        return
    }
})