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

namespace Theriapolis.Core.Dungeons;

/// <summary>
/// Phase 7 M2 — pre-instantiated population of a generated dungeon. Holds:
///   - The list of <see cref="DungeonSpawn"/> entries (one per encounter
///     slot in any room) — coordinates + template + role tag.
///   - The list of <see cref="DungeonContainer"/> entries (one per
///     container slot) — coordinates + table id + pre-rolled item drops.
///
/// Generated alongside (and from) a <see cref="Dungeon"/> via
/// <see cref="DungeonPopulator.Populate"/>. Living combatants and item
/// pickups derive from these records on first dungeon entry; the
/// <see cref="Theriapolis.Core.Persistence.DungeonStateSnapshot"/>
/// persists which entries have been resolved (cleared / looted) so
/// re-entry doesn't re-spawn them.
/// </summary>
public sealed class DungeonPopulation
{
    public DungeonSpawn[]     Spawns     { get; }
    public DungeonContainer[] Containers { get; }

    public DungeonPopulation(DungeonSpawn[] spawns, DungeonContainer[] containers)
    {
        Spawns     = spawns;
        Containers = containers;
    }
}

/// <summary>
/// One <c>@</c> encounter slot in a generated dungeon — resolved to the
/// concrete NPC template that fills it. The dungeon coords are absolute
/// (within the dungeon's tile array, not template-local).
/// </summary>
public readonly record struct DungeonSpawn(
    /// <summary>Index into <see cref="Dungeon.Rooms"/> the spawn lives in.</summary>
    int             RoomId,
    /// <summary>Dungeon-local tile-X.</summary>
    int             X,
    /// <summary>Dungeon-local tile-Y.</summary>
    int             Y,
    /// <summary>The chosen NPC template. Caller instantiates from this.</summary>
    NpcTemplateDef  Template,
    /// <summary>Spawn-kind tag the slot declared (PoiGuard / WildAnimal / Brigand / Boss).</summary>
    string          Kind);

/// <summary>
/// One <c>C</c> container slot in a generated dungeon — resolved to the
/// concrete loot drop. Populated at generation time so the same
/// <c>(worldSeed, poiId, slotIdx)</c> always rolls identical items.
/// </summary>
public readonly record struct DungeonContainer(
    /// <summary>Index into <see cref="Dungeon.Rooms"/> the container is in.</summary>
    int             RoomId,
    /// <summary>Dungeon-local tile-X.</summary>
    int             X,
    /// <summary>Dungeon-local tile-Y.</summary>
    int             Y,
    /// <summary>Loot-table id consulted at populate time.</summary>
    string          TableId,
    /// <summary>Pre-rolled item drops, ready to transfer on player loot.</summary>
    ItemInstance[]  Drops,
    /// <summary>True when the slot's grid char declared <c>locked</c>.</summary>
    bool            Locked,
    /// <summary>Lock difficulty tier ("trivial"/"easy"/"medium"/"hard"; empty when unlocked).</summary>
    string          LockTier);
