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

namespace Theriapolis.Core.Loot;

/// <summary>
/// Phase 7 M2 — container-level deterministic loot rolls. Wraps
/// <see cref="LootRoller"/> with a per-container <see cref="SeededRng"/>
/// derived from the dungeon's layout seed and the container's slot index,
/// so the same <c>(worldSeed, poiId, slotIdx)</c> always rolls the same
/// items.
///
/// Per Phase 7 plan §4.4 / §5.5:
///   <c>lootContainerSeed = dungeonLayoutSeed ^ C.RNG_DUNGEON_LOOT ^ slotIdx</c>
///
/// The <see cref="LootRoller"/> path is the encounter-drop pipeline (uses
/// the encounter's RNG); this path is for static dungeon containers and
/// does not advance any encounter-time stream.
/// </summary>
public static class LootGenerator
{
    /// <summary>
    /// Roll a single container's contents.
    /// </summary>
    /// <param name="tableId">Loot-table id (e.g. <c>loot_dungeon_imperium_t2</c>).</param>
    /// <param name="containerSeed">
    /// Per-container seed. Caller is expected to derive this as
    /// <c>worldSeed ^ C.RNG_DUNGEON_LAYOUT ^ poiId ^ C.RNG_DUNGEON_LOOT ^ slotIdx</c>.
    /// </param>
    /// <param name="tables">Loot-table dictionary from <see cref="ContentResolver.LootTables"/>.</param>
    /// <param name="items">Item dictionary from <see cref="ContentResolver.Items"/>.</param>
    /// <returns>An array of <see cref="ItemInstance"/> ready to drop into an inventory.</returns>
    public static ItemInstance[] RollContainer(
        string tableId,
        ulong containerSeed,
        IReadOnlyDictionary<string, LootTableDef> tables,
        IReadOnlyDictionary<string, ItemDef> items)
    {
        var rng = new SeededRng(containerSeed);
        var drops = LootRoller.Roll(tableId, tables, items, rng);
        var result = new ItemInstance[drops.Count];
        for (int i = 0; i < drops.Count; i++)
            result[i] = new ItemInstance(drops[i].Def, drops[i].Qty);
        return result;
    }

    /// <summary>
    /// Convenience overload that resolves the per-container seed from the
    /// dungeon layout seed + slot index per the Phase 7 dice contract.
    /// </summary>
    public static ItemInstance[] RollContainer(
        string tableId,
        ulong dungeonLayoutSeed,
        int slotIdx,
        IReadOnlyDictionary<string, LootTableDef> tables,
        IReadOnlyDictionary<string, ItemDef> items)
    {
        ulong containerSeed = dungeonLayoutSeed ^ C.RNG_DUNGEON_LOOT ^ (ulong)slotIdx;
        return RollContainer(tableId, containerSeed, tables, items);
    }
}
