using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using {{ namespace }}.Auth;
using {{ namespace }}.Auth.Strategies;
using {{ namespace }}.Http;

namespace {{ namespace }}.Core;

public static partial class ServiceCollectionExtensions
{
    /// <summary>
    /// REQ-1.2.2/REQ-1.2.3: one <see cref="IAuthStrategy"/> DI registration
    /// per discovered auth scheme, keyed by the <see cref="AuthMethod"/>
    /// enum value itself (.NET 8+ keyed services), plus the
    /// <see cref="AuthManager"/> that resolves whichever one is active.
    /// When no scheme was discovered, <see cref="AuthMethod.None"/> is the
    /// enum's only member (see `Core/Config.cs`) and <see cref="StubAuthStrategy"/>
    /// is registered under it — mirrors `targets::python`'s
    /// `strategy_for()` dict-get-with-default, just resolved at
    /// registration time instead of on every lookup.
    /// </summary>
    static partial void AddAuthStrategies(IServiceCollection services)
    {
{%- if auth_methods | length == 0 %}
        services.AddKeyedSingleton<IAuthStrategy, StubAuthStrategy>(AuthMethod.None);
{%- endif %}
{%- for method in auth_methods %}
        services.AddKeyedSingleton<IAuthStrategy, {{ method.class_name }}AuthStrategy>(AuthMethod.{{ method.class_name }});
{%- endfor %}
        // The container built from this collection implements
        // IKeyedServiceProvider automatically (.NET 8+), but that alone
        // doesn't make it *resolvable* as a constructor-injected
        // dependency — AuthManager takes IKeyedServiceProvider directly,
        // so it must be registered explicitly too.
        services.AddSingleton<IKeyedServiceProvider>(sp => (IKeyedServiceProvider)sp);
        services.AddSingleton<AuthManager>();

        // HTTP transport only: IHttpContextAccessor is safe to register
        // even for the stdio/CLI hosts that also call AddMcpifyServices —
        // it only ever resolves a non-null HttpContext from within
        // ASP.NET Core's own per-request pipeline (MapMcp's tool-call
        // dispatch included), so HttpRequestCredentialProvider correctly
        // reports "no override" everywhere else.
        services.AddHttpContextAccessor();
        services.AddScoped<IRequestCredentialProvider, HttpRequestCredentialProvider>();
    }
}
