EntityEvents.spawned(event => {
    const { entity, server } = event
    if (!entity.isMonster()) return
    
    // Ultra Blight Chance: 1 in 714
    if (Math.random() < 0.00125) {
        entity.addTag('is_ultra_blight')
        
        server.scheduleInTicks(1, callback => {
            if (!entity.alive) return
            
            // Core Stats
            entity.maxHealth = entity.maxHealth * 6
            entity.health = entity.maxHealth
            entity.setAttributeBaseValue('minecraft:generic.movement_speed', entity.getAttributeValue('minecraft:generic.movement_speed') * 2.56)
            
            let currentDamage = entity.getAttributeValue('minecraft:generic.attack_damage')
            if (currentDamage > 0) {
                entity.setAttributeBaseValue('minecraft:generic.attack_damage', currentDamage * 3.0)
            }

            // Uncheesable: Max Knockback Resistance
            entity.setAttributeBaseValue('minecraft:generic.knockback_resistance', 1.0)

            // Visuals
            let rawName = entity.type.split(':')[1].replace('_', ' ').replace(/^\w/, (c) => c.toUpperCase())
            entity.customName = Text.of("ULTRA BLIGHTED " + rawName).red().bold().italic()
            
            // Potion Effects (Fire Res added here)
            entity.potionEffects.add('minecraft:fire_resistance', 2000000, 0, false, false)
            entity.potionEffects.add('minecraft:resistance', 2000000, 1, false, false)
        })
    }
})

LevelEvents.tick(event => {
    const { level, server } = event
    
    level.getEntities().forEach(entity => {
        if (entity.tags.contains('is_ultra_blight') && entity.alive) {
            
            // Visual Pulse (Every 10 ticks)
            if (level.time % 10 == 0) {
                level.spawnParticles('cataclysm:trap_flame', true, entity.x, entity.y + 1, entity.z, 0.3, 0.5, 0.3, 5, 0.05)
                level.spawnParticles('minecraft:sonic_boom', true, entity.x, entity.y + 1, entity.z, 0, 0, 0, 1, 0)
            }

            // Anti-Cheese Logic (Every 40-60 ticks)
            if (level.time % 40 == 0) {
                let target = level.getPlayers().find(p => p.distanceToEntity(entity) < 25)
                if (target) {
                    // 1. Anti-Tower Teleport
                    if (target.y - entity.y > 5) {
                        entity.setPosition(target.x, target.y, target.z)
                        server.runCommandSilent(`playsound minecraft:entity.enderman.teleport host @a ${entity.x} ${entity.y} ${entity.z} 1 1`)
                        server.runCommandSilent(`particle minecraft:portal ${entity.x} ${entity.y} ${entity.z} 1 1 1 0.1 50`)
                    }
                    
                    // 2. Fireball Attack (Max 16 blocks)
                    if (entity.distanceToEntity(target) < 16 && level.time % 60 == 0) {
                        let dx = (target.x - entity.x) * 0.2
                        let dy = (target.y + 1 - (entity.y + 2)) * 0.2
                        let dz = (target.z - entity.z) * 0.2
                        server.runCommandSilent(`execute at ${entity.uuid} run summon fireball ~ ~2 ~ {ExplosionPower:2,power:[${dx},${dy},${dz}]}`)
                        server.runCommandSilent(`playsound minecraft:entity.ghast.warn host @a ${entity.x} ${entity.y} ${entity.z} 1 0.5`)
                    }
                }
            }
        }
    })
})

// Anti-Projectile Shield (75% Chance)
EntityEvents.hurt(event => {
    const { entity, source } = event
    if (entity.tags.contains('is_ultra_blight')) {
        if (source.type === 'arrow' || source.type === 'thrown' || source.type === 'trident') {
            if (Math.random() < 0.75) {
                event.cancel()
                entity.level.spawnParticles('minecraft:crit', true, entity.x, entity.y + 1, entity.z, 0.5, 0.5, 0.5, 10, 0.1)
                entity.level.playSound(null, entity.x, entity.y, entity.z, 'minecraft:block.bell.use', 'hostile', 1, 2)
            }
        }
    }
})

EntityEvents.death(event => {
    const { entity, server } = event
    if (entity.tags.contains('is_ultra_blight')) {
        server.tell(Text.of("AN ULTRA BLIGHT HAS BEEN DEFEATED!").gold().bold())
        
        server.runCommandSilent(`execute at ${entity.uuid} run particle minecraft:flash ~ ~1 ~ 0 0 0 0 1`)
        server.runCommandSilent(`execute at ${entity.uuid} run playsound minecraft:entity.generic.explode world @a ~ ~ ~ 2 1`)
        
        // Guaranteed Loot Drops
        server.runCommandSilent(`execute at ${entity.uuid} run summon item ~ ~ ~ {Item:{id:"born_in_chaos_v1:chocolate_heart",Count:3b}}`)
        server.runCommandSilent(`execute at ${entity.uuid} run summon item ~ ~ ~ {Item:{id:"minecraft:enchanted_book",Count:1b,tag:{StoredEnchantments:[{id:"minecraft:mending",lvl:1s}]}}}`)
        server.runCommandSilent(`execute at ${entity.uuid} run summon item ~ ~ ~ {Item:{id:"minecraft:enchanted_golden_apple",Count:1b}}`)
        server.runCommandSilent(`execute at ${entity.uuid} run summon item ~ ~ ~ {Item:{id:"bountifulbaubles:treasure_bag",Count:2b}}`)
        server.runCommandSilent(`execute at ${entity.uuid} run summon item ~ ~ ~ {Item:{id:"minecraft:netherite_ingot",Count:1b}}`)

        // ~30 Levels of XP
        for (let i = 0; i < 14; i++) {
            server.runCommandSilent(`execute at ${entity.uuid} run summon experience_orb ~ ~ ~ {Value:100}`)
        }
    }
})