using Theriapolis.Core.Data;
using Theriapolis.Core.Util;

namespace Theriapolis.Core.Loot;

/// <summary>
/// Pure deterministic loot roller. Given a table id and an RNG (typically
/// the encounter's <see cref="Rules.Combat.Encounter.RollDie"/>), produces
/// the list of (itemDef, qty) tuples to drop.
///
/// Determinism: dice come from the encounter RNG so save+load round-trips
/// produce identical drops — important for the autosave_combat retry slot.
/// </summary>
public static class LootRoller
{
    public sealed record DropResult(ItemDef Def, int Qty);

    /// <summary>
    /// Roll <paramref name="tableId"/> against the supplied RNG. Returns an
    /// empty list when the table id is empty/unknown.
    /// </summary>
    public static List<DropResult> Roll(
        string tableId,
        IReadOnlyDictionary<string, LootTableDef> tables,
        IReadOnlyDictionary<string, ItemDef> items,
        SeededRng rng)
    {
        var results = new List<DropResult>();
        if (string.IsNullOrEmpty(tableId) || !tables.TryGetValue(tableId, out var table))
            return results;

        foreach (var drop in table.Drops)
        {
            // Independent chance roll per drop.
            if (rng.NextFloat() > drop.Chance) continue;
            if (!items.TryGetValue(drop.ItemId, out var def)) continue;

            int qty;
            if (drop.QtyMax <= drop.QtyMin) qty = System.Math.Max(1, drop.QtyMin);
            else                            qty = rng.NextInt(drop.QtyMin, drop.QtyMax + 1);

            results.Add(new DropResult(def, qty));
        }
        return results;
    }
}
