EntityEvents.spawned(event => {
    const { entity, server } = event
    if (!entity.isMonster()) return
    
    if (Math.random() < 0.005) {
        entity.addTag('is_blight')
        
        server.scheduleInTicks(1, callback => {
            if (!entity.alive) return
            entity.maxHealth = entity.maxHealth * 3 
            entity.health = entity.maxHealth
            
            let currentSpeed = entity.getAttributeValue('minecraft:generic.movement_speed')
            entity.setAttributeBaseValue('minecraft:generic.movement_speed', currentSpeed * 1.6)

            let currentDamage = entity.getAttributeValue('minecraft:generic.attack_damage')
            if (currentDamage > 0) {
                entity.setAttributeBaseValue('minecraft:generic.attack_damage', currentDamage * 1.5)
            }

            let rawName = entity.type.split(':')[1].replace('_', ' ').replace(/^\w/, (c) => c.toUpperCase())
            entity.customName = Text.of("Blighted " + rawName).darkPurple()
            
            entity.potionEffects.add('minecraft:fire_resistance', 2000000, 0, false, false)
        })
    }
})

LevelEvents.tick(event => {
    const { level } = event
    // Less frequent: only triggers every 10 ticks (twice per second)
    if (level.time % 10 != 0) return

    level.getEntities().forEach(entity => {
        if (entity.tags.contains('is_blight') && entity.alive) {
            let playerClose = level.getPlayers().find(p => p.distanceToEntity(entity) < 64)
            if (!playerClose) return
            
            // Reduced Radius: the 0, 0, 0 and 0.1, 0.2, 0.1 make the particles stay tight to the mob's body
            level.spawnParticles('minecraft:sonic_boom', true, entity.x, entity.y + 1, entity.z, 0, 0, 0, 1, 0)
            level.spawnParticles('minecraft:glow_squid_ink', true, entity.x, entity.y + 1, entity.z, 0.1, 0.2, 0.1, 2, 0.01)
        }
    })
})

EntityEvents.death(event => {
    const { entity, server } = event
    if (entity.tags.contains('is_blight')) {
        
        server.tell(Text.of("A Blighted soul has been purged!").lightPurple().italic())
        
        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.lightning_bolt.thunder world @a ~ ~ ~ 1 1`)
        server.runCommandSilent(`execute at ${entity.uuid} run playsound minecraft:entity.lightning_bolt.impact world @a ~ ~ ~ 1 1`)
        
        server.runCommandSilent(`execute at ${entity.uuid} run summon item ~ ~ ~ {Item:{id:"born_in_chaos_v1:chocolate_heart",Count:1b}}`)
        
        if (Math.random() < 0.50) {
            server.runCommandSilent(`execute at ${entity.uuid} run summon item ~ ~ ~ {Item:{id:"minecraft:enchanted_book",Count:1b,tag:{StoredEnchantments:[{id:"minecraft:mending",lvl:1s}]}}}`)
        }

        if (Math.random() < 0.15) {
            server.runCommandSilent(`execute at ${entity.uuid} run summon item ~ ~ ~ {Item:{id:"minecraft:golden_apple",Count:1b}}`)
        }

        if (Math.random() < 0.10) {
            server.runCommandSilent(`execute at ${entity.uuid} run summon item ~ ~ ~ {Item:{id:"bountifulbaubles:treasure_bag",Count:1b}}`)
        }
        
        if (Math.random() < 0.03) {
            server.runCommandSilent(`execute at ${entity.uuid} run summon item ~ ~ ~ {Item:{id:"heart_crystals:heart_crystal",Count:1b}}`)
        }

        for (let i = 0; i < 4; i++) {
            server.runCommandSilent(`execute at ${entity.uuid} run summon experience_orb ~ ~ ~ {Value:45}`)
        }
    }
})