using System.Collections.Generic;
using UnityEngine;
using YachtDice.Categories;
using YachtDice.Dice;
using YachtDice.Modifiers.Runtime;
using YachtDice.Scoring;

namespace YachtDice.Modifiers.Core
{
    public class ModifierContext
    {
        // Scoring data
        public int BaseScore;
        public int FlatBonus;
        public float Multiplier = 1f;
        public float PostMultiplier = 1f;

        /// <summary>Абстрактные дайсы (основной API).</summary>
        public IReadOnlyList<IDie> Dice;

        /// <summary>Значения дайсов (обратная совместимость с существующими модификаторами).</summary>
        public int[] DiceValues;

        /// <summary>Категория, в которую записывается результат.</summary>
        public CategoryDefinition Category;

        // Game state (read-only snapshot)
        public int CurrentRoll;
        public int CurrentTurn;
        public int PlayerCurrency;
        public IReadOnlyList<ModifierInstance> AllActiveModifiers;

        // Trigger info
        public TriggerType Trigger;

        // Side-effect accumulators
        public int CurrencyDelta;

        // Debug trace (populated when tracing is enabled)
        public List<string> DebugLog;

        public int FinalScore => Mathf.FloorToInt((BaseScore + FlatBonus) * Multiplier * PostMultiplier);

        public ScoreResult ToScoreResult()
        {
            return new ScoreResult
            {
                BaseScore = BaseScore,
                FlatBonus = FlatBonus,
                Multiplier = Multiplier * PostMultiplier,
                DiceValues = DiceValues,
                Category = Category,
            };
        }

        public static ModifierContext CreateForScoring(
            int baseScore,
            IReadOnlyList<IDie> dice,
            CategoryDefinition category,
            int currentRoll,
            int currentTurn,
            int playerCurrency,
            IReadOnlyList<ModifierInstance> activeModifiers)
        {
            return new ModifierContext
            {
                BaseScore = baseScore,
                Dice = dice,
                DiceValues = DiceCheckUtility.ExtractValues(dice),
                Category = category,
                CurrentRoll = currentRoll,
                CurrentTurn = currentTurn,
                PlayerCurrency = playerCurrency,
                AllActiveModifiers = activeModifiers,
            };
        }
    }
}
