using Theriapolis.Core.Rules.Combat;

namespace Theriapolis.Core.Entities.Ai;

/// <summary>
/// One NPC behavior — what does this NPC do on its turn? Behaviors are
/// dispatched by id (the template's <c>behavior</c> field) via
/// <see cref="BehaviorRegistry"/>. Each behavior must produce its turn's
/// actions within bounded time — no recursion, no while-true loops; if no
/// valid action is available, end the turn.
/// </summary>
public interface INpcBehavior
{
    /// <summary>
    /// Take one turn for <paramref name="self"/>. Behaviors call into
    /// <see cref="Resolver"/> to mutate the encounter, then return — the
    /// caller (typically <see cref="Encounter"/>'s turn pump) calls
    /// <see cref="Encounter.EndTurn"/> after this returns.
    /// </summary>
    void TakeTurn(Combatant self, AiContext ctx);
}

/// <summary>
/// Maps behavior ids (the strings in <c>npc_templates.json</c>'s
/// <c>behavior</c> field) to their <see cref="INpcBehavior"/>
/// implementation. Phase 5 M5 ships three: <c>brigand</c>,
/// <c>wild_animal</c>, <c>poi_guard</c>. Unknown ids fall back to
/// <c>brigand</c> with a debug log.
/// </summary>
public static class BehaviorRegistry
{
    private static readonly System.Collections.Generic.Dictionary<string, INpcBehavior> _impls
        = new(System.StringComparer.OrdinalIgnoreCase)
        {
            ["brigand"]     = new BrigandBehavior(),
            ["wild_animal"] = new WildAnimalBehavior(),
            ["poi_guard"]   = new PoiGuardBehavior(),
            ["patrol"]      = new BrigandBehavior(),  // patrol shares brigand combat behavior
        };

    public static INpcBehavior For(string id)
    {
        return _impls.TryGetValue(id, out var b) ? b : _impls["brigand"];
    }
}
