namespace Theriapolis.Core.World;

public enum FactionId : byte
{
    CovenantEnforcers = 0,
    Inheritors        = 1,
    ThornCouncil      = 2,
}

/// <summary>
/// Per-tile influence map for the three primary factions.
/// Influence[factionIndex, x, y] ∈ [0, 1].
/// </summary>
public sealed class FactionInfluenceMap
{
    public const int FactionCount = 3;

    // Flat array: [faction * W * H + y * W + x]
    private readonly float[] _data;
    private const int W = C.WORLD_WIDTH_TILES;
    private const int H = C.WORLD_HEIGHT_TILES;

    public FactionInfluenceMap()
    {
        _data = new float[FactionCount * W * H];
    }

    public float Get(int faction, int x, int y) => _data[faction * W * H + y * W + x];
    public void  Set(int faction, int x, int y, float value) =>
        _data[faction * W * H + y * W + x] = value;

    public void Add(int faction, int x, int y, float delta)
    {
        int idx = faction * W * H + y * W + x;
        _data[idx] = Math.Max(0f, Math.Min(1f, _data[idx] + delta));
    }

    /// <summary>Returns the index of the faction with highest influence at this tile, or -1 if all zero.</summary>
    public int DominantFaction(int x, int y)
    {
        float best = 0f;
        int idx = -1;
        for (int f = 0; f < FactionCount; f++)
        {
            float v = Get(f, x, y);
            if (v > best) { best = v; idx = f; }
        }
        return idx;
    }
}
