using UnityEngine;
[CreateAssetMenu(menuName = "Actions/Attack Strategy Action")]
public class AttackStrategyAction : EnemyAction
{
    public override float CalculateUtility(EnemyDecisionContext ctx)
    {
        if (!ctx.Entity.Brain.Combat.CanAttack()) return 0f;
        
        float dist = ctx.DistanceToPlayer;
        float distToIdeal = Mathf.Abs(dist - ctx.Config.idealAttackDistance);
        float distScore = Mathf.InverseLerp(ctx.Config.idealAttackDistance + 1f, 0f, distToIdeal);
        distScore = Mathf.Clamp01(distScore);

        float roleBias = (ctx.CurrentRole == EnemyRole.Attack) ? 0.5f : 0.0f;

        float timeFactor = Mathf.InverseLerp(0f, ctx.Config.attackCooldown * 2f, ctx.TimeSinceLastAttack);
        
        float baseDesire = 0.1f;
        
        float utility = baseDesire +
                        distScore * 0.5f +
                        roleBias +
                        timeFactor * 0.3f;

        return Mathf.Clamp01(utility);
    }

    public override void Execute(EnemyBrain brain, EnemyDecisionContext ctx)
    {
        EnemyAbility bestAbility = null;
        float bestUtility = -1f;
        foreach (var ability in ctx.Config.abilities)
        {
            float utility = ability.ComputeUtility(ctx);
            if (utility > bestUtility) bestAbility = ability;
        }

        if (bestAbility != null)
        {
            brain.Combat.PerformAbility(bestAbility, ctx);
        }else{
            Debug.LogWarning("Ability can't be used since it's null!!!");
        }
    }
}
