package model

type EnemyType string

const (
	EnemyWolf             EnemyType = "wolf"
	EnemyBoar             EnemyType = "boar"
	EnemyZombie           EnemyType = "zombie"
	EnemySpider           EnemyType = "spider"
	EnemyOrc              EnemyType = "orc"
	EnemySkeletonArcher   EnemyType = "skeleton_archer"
	EnemyBattleLizard     EnemyType = "battle_lizard"
	EnemyFireDemon        EnemyType = "fire_demon"
	EnemyIceGuardian      EnemyType = "ice_guardian"
	EnemySkeletonKing     EnemyType = "skeleton_king"
	EnemyWaterElement     EnemyType = "water_element"
	EnemyForestWarden     EnemyType = "forest_warden"
	EnemyLightningTitan   EnemyType = "lightning_titan"
)

type SpecialAbility string

const (
	AbilityBurn           SpecialAbility = "burn"            // DoT fire damage
	AbilitySlow           SpecialAbility = "slow"            // -40% movement speed (Water Element)
	AbilityCritical       SpecialAbility = "critical"        // chance for double damage
	AbilityPoison         SpecialAbility = "poison"          // DoT poison damage
	AbilityFreeze         SpecialAbility = "freeze"          // -50% attack speed (generic)
	AbilityIceSlow        SpecialAbility = "ice_slow"        // -20% attack speed (Ice Guardian per spec)
	AbilityStun           SpecialAbility = "stun"            // no attacks for 2 sec
	AbilityDodge          SpecialAbility = "dodge"           // chance to avoid incoming damage
	AbilityRegen          SpecialAbility = "regen"           // regenerate HP over time
	AbilityBurst          SpecialAbility = "burst"           // every Nth attack deals multiplied damage
	AbilityChainLightning SpecialAbility = "chain_lightning" // 3x damage after 5 attacks
	AbilitySummon         SpecialAbility = "summon"          // summons minions
)

type Enemy struct {
	ID               int64            `json:"id"`
	Type             EnemyType        `json:"type"`
	Name             string           `json:"name"`
	HP               int              `json:"hp"`
	MaxHP            int              `json:"maxHp"`
	Attack           int              `json:"attack"`
	Defense          int              `json:"defense"`
	Speed            float64          `json:"speed"`       // attacks per second
	CritChance       float64          `json:"critChance"` // 0.0 to 1.0
	MinLevel         int              `json:"minLevel"`
	MaxLevel         int              `json:"maxLevel"`
	XPReward         int64            `json:"xpReward"`
	GoldReward       int64            `json:"goldReward"`
	SpecialAbilities []SpecialAbility `json:"specialAbilities,omitempty"`
	IsElite          bool             `json:"isElite"`
	AttackCount      int              `json:"-"` // tracks attacks for burst/chain abilities
}

// IsAlive returns true if the enemy has HP remaining.
func (e *Enemy) IsAlive() bool {
	return e.HP > 0
}

// HasAbility checks if the enemy possesses a given special ability.
func (e *Enemy) HasAbility(a SpecialAbility) bool {
	for _, ab := range e.SpecialAbilities {
		if ab == a {
			return true
		}
	}
	return false
}

// EnemyTemplates defines base stats for each enemy type.
// These are used when spawning new enemies; actual instances may have scaled stats.
var EnemyTemplates = map[EnemyType]Enemy{
	// --- Basic enemies ---
	EnemyWolf: {
		Type: EnemyWolf, Name: "Forest Wolf",
		MaxHP: 45, Attack: 9, Defense: 4, Speed: 1.8, CritChance: 0.05,
		MinLevel: 1, MaxLevel: 5,
		XPReward: 1, GoldReward: 1,
	},
	EnemyBoar: {
		Type: EnemyBoar, Name: "Wild Boar",
		MaxHP: 65, Attack: 18, Defense: 7, Speed: 0.8, CritChance: 0.08,
		MinLevel: 2, MaxLevel: 6,
		XPReward: 1, GoldReward: 1,
	},
	EnemyZombie: {
		Type: EnemyZombie, Name: "Rotting Zombie",
		MaxHP: 95, Attack: 16, Defense: 7, Speed: 0.5,
		MinLevel: 3, MaxLevel: 8,
		XPReward: 1, GoldReward: 1,
		SpecialAbilities: []SpecialAbility{AbilityPoison},
	},
	EnemySpider: {
		Type: EnemySpider, Name: "Cave Spider",
		MaxHP: 38, Attack: 16, Defense: 3, Speed: 2.0, CritChance: 0.15,
		MinLevel: 4, MaxLevel: 9,
		XPReward: 1, GoldReward: 1,
		SpecialAbilities: []SpecialAbility{AbilityCritical},
	},
	EnemyOrc: {
		Type: EnemyOrc, Name: "Orc Warrior",
		MaxHP: 110, Attack: 21, Defense: 12, Speed: 1.0, CritChance: 0.05,
		MinLevel: 5, MaxLevel: 12,
		XPReward: 1, GoldReward: 1,
		SpecialAbilities: []SpecialAbility{AbilityBurst},
	},
	EnemySkeletonArcher: {
		Type: EnemySkeletonArcher, Name: "Skeleton Archer",
		MaxHP: 90, Attack: 24, Defense: 10, Speed: 1.3, CritChance: 0.06,
		MinLevel: 6, MaxLevel: 14,
		XPReward: 1, GoldReward: 1,
		SpecialAbilities: []SpecialAbility{AbilityDodge},
	},
	EnemyBattleLizard: {
		Type: EnemyBattleLizard, Name: "Battle Lizard",
		MaxHP: 140, Attack: 24, Defense: 18, Speed: 0.7, CritChance: 0.03,
		MinLevel: 7, MaxLevel: 15,
		XPReward: 1, GoldReward: 1,
		SpecialAbilities: []SpecialAbility{AbilityRegen},
	},

	// --- Elite enemies ---
	EnemyFireDemon: {
		Type: EnemyFireDemon, Name: "Fire Demon",
		MaxHP: 230, Attack: 34, Defense: 20, Speed: 1.2, CritChance: 0.10,
		MinLevel: 10, MaxLevel: 20,
		XPReward: 1, GoldReward: 1, IsElite: true,
		SpecialAbilities: []SpecialAbility{AbilityBurn},
	},
	EnemyIceGuardian: {
		Type: EnemyIceGuardian, Name: "Ice Guardian",
		MaxHP: 280, Attack: 32, Defense: 28, Speed: 0.7, CritChance: 0.04,
		MinLevel: 12, MaxLevel: 22,
		XPReward: 1, GoldReward: 1, IsElite: true,
		SpecialAbilities: []SpecialAbility{AbilityIceSlow},
	},
	EnemySkeletonKing: {
		Type: EnemySkeletonKing, Name: "Skeleton King",
		MaxHP: 420, Attack: 48, Defense: 30, Speed: 0.9, CritChance: 0.08,
		MinLevel: 15, MaxLevel: 25,
		XPReward: 1, GoldReward: 1, IsElite: true,
		SpecialAbilities: []SpecialAbility{AbilityRegen, AbilitySummon},
	},
	EnemyWaterElement: {
		Type: EnemyWaterElement, Name: "Water Element",
		MaxHP: 520, Attack: 42, Defense: 24, Speed: 0.8, CritChance: 0.05,
		MinLevel: 18, MaxLevel: 28,
		XPReward: 2, GoldReward: 1, IsElite: true,
		SpecialAbilities: []SpecialAbility{AbilitySlow},
	},
	EnemyForestWarden: {
		Type: EnemyForestWarden, Name: "Forest Warden",
		MaxHP: 700, Attack: 38, Defense: 40, Speed: 0.5, CritChance: 0.03,
		MinLevel: 20, MaxLevel: 30,
		XPReward: 2, GoldReward: 1, IsElite: true,
		SpecialAbilities: []SpecialAbility{AbilityRegen},
	},
	EnemyLightningTitan: {
		Type: EnemyLightningTitan, Name: "Lightning Titan",
		MaxHP: 650, Attack: 56, Defense: 30, Speed: 1.5, CritChance: 0.12,
		MinLevel: 25, MaxLevel: 35,
		XPReward: 3, GoldReward: 2, IsElite: true,
		SpecialAbilities: []SpecialAbility{AbilityStun, AbilityChainLightning},
	},
}

func SetEnemyTemplates(next map[EnemyType]Enemy) {
	if len(next) == 0 {
		return
	}
	EnemyTemplates = next
}
