using UnityEngine;

public class ZoneEntity : MonoBehaviour
{
    private float _damage;
    private float _radius;
    private float _duration;
    private float _tickInterval;
    private DebuffData[] _debuffs;

    private float _lifeTimer;
    private float _tickAccumulator;

    public void Init(float damage, float radius, float duration, float tickInterval, DebuffData[] debuffs)
    {
        _damage = damage;
        _radius = radius;
        _duration = duration;
        _tickInterval = tickInterval > 0 ? tickInterval : 1f;
        _debuffs = debuffs;
    }

    private void Update()
    {
        _lifeTimer += Time.deltaTime;
        _tickAccumulator += Time.deltaTime;

        if (_tickAccumulator >= _tickInterval)
        {
            _tickAccumulator -= _tickInterval;
            ApplyTickDamage();
        }

        if (_lifeTimer >= _duration)
        {
            Destroy(gameObject);
        }
    }

    private void ApplyTickDamage()
    {
        Collider[] hits = Physics.OverlapSphere(transform.position, _radius);
        foreach (Collider col in hits)
        {
            IDamageable target = col.GetComponent<IDamageable>();
            if (target == null) continue;

            target.TakeDamage(Mathf.RoundToInt(_damage), transform);

            if (_debuffs != null)
            {
                StatusEffectReceiver receiver = col.GetComponent<StatusEffectReceiver>();
                if (receiver != null)
                {
                    foreach (var debuff in _debuffs)
                        receiver.ApplyDebuff(debuff);
                }
            }
        }
    }
}
