namespace Theriapolis.Core.Persistence;

/// <summary>
/// Mid-encounter snapshot. Present in <see cref="SaveBody"/> only when the
/// player saved during combat. On load, the live PlayScreen re-creates a
/// <see cref="Rules.Combat.Encounter"/> with the same participants and
/// calls <c>ResumeRolls(RollCount)</c> so the dice stream continues from
/// the same sequence point — see Phase 5 plan §5.
/// </summary>
public sealed class EncounterState
{
    public ulong EncounterId      { get; set; }
    public int   RollCount        { get; set; }
    public int   CurrentTurnIndex { get; set; }
    public int   RoundNumber      { get; set; }
    public int[] InitiativeOrder  { get; set; } = System.Array.Empty<int>();
    public CombatantSnapshot[] Combatants { get; set; } = System.Array.Empty<CombatantSnapshot>();
}

/// <summary>One combatant in the saved encounter.</summary>
public sealed class CombatantSnapshot
{
    public int    Id        { get; set; }
    /// <summary>Display name (used to verify the same combatant on resume).</summary>
    public string Name      { get; set; } = "";
    /// <summary>True if this combatant is the player. False = NPC.</summary>
    public bool   IsPlayer  { get; set; }
    /// <summary>For NPC combatants: the chunk + spawn index that produced them, used to find the live <c>NpcActor</c> on resume.</summary>
    public int?   NpcChunkX { get; set; }
    public int?   NpcChunkY { get; set; }
    public int?   NpcSpawnIndex { get; set; }
    /// <summary>For NPC combatants: the template id (used as fallback identity if chunk lookup fails).</summary>
    public string NpcTemplateId { get; set; } = "";
    public int    CurrentHp { get; set; }
    public float  PositionX { get; set; }
    public float  PositionY { get; set; }
    /// <summary>Active conditions as enum byte values.</summary>
    public byte[] Conditions { get; set; } = System.Array.Empty<byte>();
}

/// <summary>
/// Per-chunk roster delta — which spawn indices have been killed (or
/// otherwise consumed). Layered on top of the chunk's deterministic spawn
/// list so reloading a chunk doesn't resurrect dead enemies.
/// </summary>
public sealed class NpcRosterState
{
    public List<NpcChunkDelta> ChunkDeltas { get; set; } = new();
}

public sealed class NpcChunkDelta
{
    public int   ChunkX           { get; set; }
    public int   ChunkY           { get; set; }
    /// <summary>Spawn-list indices in this chunk whose NPC has died.</summary>
    public int[] KilledSpawnIndices { get; set; } = System.Array.Empty<int>();
}
