package combat

import "time"

// TargetType determines how a skill selects its target.
type TargetType int

const (
	TargetSelf        TargetType = iota
	TargetSingleEnemy            // requires a valid enemy entity ID
	TargetSingleAlly             // requires a valid ally entity ID
	TargetAoEGround              // requires a ground position
	TargetAoETarget              // AoE centered on target entity
)

// EffectType determines what a skill effect does.
type EffectType int

const (
	EffectDamage   EffectType = iota
	EffectHeal
	EffectBuff
	EffectDebuff
)

// Effect is a single outcome of a skill.
type Effect struct {
	Type         EffectType
	Value        int32         // damage amount, heal amount, or buff/debuff ID
	Duration     time.Duration // 0 for instant effects
	TickInterval time.Duration // for DoT/HoT; 0 means apply once
}

// SkillDef defines a skill's properties (loaded from data).
type SkillDef struct {
	ID         uint32
	Name       string
	Cooldown   time.Duration
	ManaCost   int32
	Range      float32     // max distance to target (0 = self only)
	TargetType TargetType
	AoERadius  float32     // for AoE skills
	CastTime   time.Duration
	Effects    []Effect
}

// SkillRegistry holds all skill definitions.
type SkillRegistry struct {
	skills map[uint32]*SkillDef
}

// NewSkillRegistry creates a registry with default skills.
func NewSkillRegistry() *SkillRegistry {
	r := &SkillRegistry{skills: make(map[uint32]*SkillDef)}
	r.registerDefaults()
	return r
}

// Get returns a skill definition by ID.
func (r *SkillRegistry) Get(id uint32) *SkillDef {
	return r.skills[id]
}

// Register adds a skill definition.
func (r *SkillRegistry) Register(s *SkillDef) {
	r.skills[s.ID] = s
}

func (r *SkillRegistry) registerDefaults() {
	// Basic attack
	r.Register(&SkillDef{
		ID:         1,
		Name:       "Basic Attack",
		Cooldown:   1 * time.Second,
		ManaCost:   0,
		Range:      3.0,
		TargetType: TargetSingleEnemy,
		Effects: []Effect{
			{Type: EffectDamage, Value: 15},
		},
	})

	// Fireball - ranged damage
	r.Register(&SkillDef{
		ID:         2,
		Name:       "Fireball",
		Cooldown:   3 * time.Second,
		ManaCost:   20,
		Range:      15.0,
		TargetType: TargetSingleEnemy,
		Effects: []Effect{
			{Type: EffectDamage, Value: 40},
		},
	})

	// Heal
	r.Register(&SkillDef{
		ID:         3,
		Name:       "Heal",
		Cooldown:   5 * time.Second,
		ManaCost:   30,
		Range:      0,
		TargetType: TargetSelf,
		Effects: []Effect{
			{Type: EffectHeal, Value: 50},
		},
	})

	// AoE - Flame Strike
	r.Register(&SkillDef{
		ID:         4,
		Name:       "Flame Strike",
		Cooldown:   8 * time.Second,
		ManaCost:   40,
		Range:      12.0,
		TargetType: TargetAoEGround,
		AoERadius:  5.0,
		Effects: []Effect{
			{Type: EffectDamage, Value: 30},
		},
	})

	// Poison (DoT debuff)
	r.Register(&SkillDef{
		ID:         5,
		Name:       "Poison",
		Cooldown:   6 * time.Second,
		ManaCost:   15,
		Range:      8.0,
		TargetType: TargetSingleEnemy,
		Effects: []Effect{
			{Type: EffectDebuff, Value: 1, Duration: 10 * time.Second, TickInterval: 2 * time.Second},
		},
	})

	// Power Buff (self buff - increases damage)
	r.Register(&SkillDef{
		ID:         6,
		Name:       "Power Up",
		Cooldown:   15 * time.Second,
		ManaCost:   25,
		Range:      0,
		TargetType: TargetSelf,
		Effects: []Effect{
			{Type: EffectBuff, Value: 2, Duration: 10 * time.Second},
		},
	})
}
