using UnityEngine;
[CreateAssetMenu(menuName = "Actions/Approach")]
public class ApproachAction : EnemyAction
{
    [SerializeField] private float maxApproachDistance = 6f;
    [SerializeField] private float minApproachDistance = 0.3f;
    
    public override float CalculateUtility(EnemyDecisionContext ctx)
    {
        // 1. Distance Score
        float dist = ctx.DistanceToPlayer;
        float tooClosePenalty = dist < minApproachDistance ? 0.0f : 1f;

        float distScore = Mathf.InverseLerp(maxApproachDistance, minApproachDistance, dist);
        distScore = Mathf.Clamp01(distScore) * tooClosePenalty;
        
        // 2. Role Bias
        float roleBias = (ctx.CurrentRole == EnemyRole.Approach) ? 0.4f : 0f;
        
        // 3. Recent Attack Penalty (The Fix)
        // If we attacked recently, we heavily penalize approaching to allow the Retreat action to take over.
        float recentAttackFrac = Mathf.Clamp01(ctx.TimeSinceLastAttack / ctx.Config.attackCooldown);
        float recentAttackPenalty = 1f - recentAttackFrac;

        float baseDesire = 0.2f;

        // Changed weights:
        // Increased penalty weight from 0.2 to 0.8
        float utility = baseDesire +
                        distScore * 0.6f +
                        roleBias -
                        (recentAttackPenalty * 0.8f); 

        return Mathf.Clamp01(utility);
    }

    public override void Execute(EnemyBrain brain, EnemyDecisionContext ctx)
    {
        brain.MoveTowardsSlot();
        brain.animationBridge.UpdateApproachState(true);
        brain.animationBridge.UpdateIdleState(false);
    }
}
