using UnityEngine;
using YachtDice.Modifiers.Core;
using YachtDice.Modifiers.Definition;
using YachtDice.Modifiers.Runtime;

namespace YachtDice.Modifiers.Conditions
{
    [CreateAssetMenu(fileName = "DiceCountCondition", menuName = "YachtDice/Modifiers/Conditions/Dice Count")]
    public class DiceCountCondition : Condition
    {
        [Tooltip("Die face value to count (1-6). 0 = any value.")]
        [SerializeField, Range(0, 6)] private int targetValue;

        [Tooltip("Minimum number of dice that must match.")]
        [SerializeField] private int minCount = 1;

        public override bool Evaluate(ModifierContext context, ModifierInstance instance)
        {
            if (context.DiceValues == null) return false;

            int count = 0;
            for (int i = 0; i < context.DiceValues.Length; i++)
            {
                if (targetValue == 0 || context.DiceValues[i] == targetValue)
                    count++;
            }
            return count >= minCount;
        }

#if UNITY_EDITOR
        public static DiceCountCondition CreateForTest(int targetValue, int minCount)
        {
            var so = CreateInstance<DiceCountCondition>();
            so.targetValue = targetValue;
            so.minCount = minCount;
            return so;
        }
#endif
    }
}
