namespace Theriapolis.Core.World.Settlements;

/// <summary>
/// Phase 6 M0 — runtime record of a single stamped building inside a
/// settlement. Created by <see cref="SettlementStamper"/> at chunk-gen time
/// and attached to the parent <see cref="World.Settlement"/>.
///
/// Buildings can straddle chunk boundaries; the footprint is in
/// world-pixel (= tactical-tile) coordinates so cross-chunk lookups
/// (e.g. "is the player inside the Millhaven inn?") work without per-chunk
/// reconstruction.
/// </summary>
public sealed class BuildingFootprint
{
    /// <summary>Unique id within the parent settlement (sequential, 0-based).</summary>
    public int Id { get; init; }

    /// <summary>Building template id (e.g. "inn_small").</summary>
    public string TemplateId { get; init; } = "";

    /// <summary>Inclusive minimum X in world-pixel space.</summary>
    public int MinX { get; init; }

    /// <summary>Inclusive minimum Y in world-pixel space.</summary>
    public int MinY { get; init; }

    /// <summary>Inclusive maximum X in world-pixel space.</summary>
    public int MaxX { get; init; }

    /// <summary>Inclusive maximum Y in world-pixel space.</summary>
    public int MaxY { get; init; }

    /// <summary>Door positions in world-pixel space (one entry per door).</summary>
    public (int X, int Y)[] Doors { get; init; } = Array.Empty<(int, int)>();

    /// <summary>Resident slots: role tag (possibly anchor-prefixed) → spawn position in world-pixel space.</summary>
    public BuildingResidentSlot[] Residents { get; init; } = Array.Empty<BuildingResidentSlot>();

    public bool ContainsTile(int worldPxX, int worldPxY)
        => worldPxX >= MinX && worldPxX <= MaxX
        && worldPxY >= MinY && worldPxY <= MaxY;
}

/// <summary>One resident slot inside a building.</summary>
public readonly struct BuildingResidentSlot
{
    /// <summary>Role tag — either generic ("innkeeper") or anchor-qualified ("millhaven.innkeeper").</summary>
    public readonly string RoleTag;

    /// <summary>Spawn point in world-pixel (tactical-tile) coordinates.</summary>
    public readonly int SpawnX;
    public readonly int SpawnY;

    /// <summary>Optional category match for procedural residents — passed through from BuildingTemplateDef.Category.</summary>
    public readonly string Category;

    public BuildingResidentSlot(string roleTag, int spawnX, int spawnY, string category)
    {
        RoleTag = roleTag;
        SpawnX  = spawnX;
        SpawnY  = spawnY;
        Category = category;
    }
}
