using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.DependencyInjection;
using ScadaLink.CentralUI.Auth;
using ScadaLink.CentralUI.Components.Shared;
using ScadaLink.CentralUI.ScriptAnalysis;

namespace ScadaLink.CentralUI;

public static class ServiceCollectionExtensions
{
    public static IServiceCollection AddCentralUI(this IServiceCollection services)
    {
        services.AddRazorComponents()
            .AddInteractiveServerComponents();

        services.AddHttpContextAccessor();

        services.AddScoped<AuthenticationStateProvider, CookieAuthenticationStateProvider>();
        services.AddCascadingAuthenticationState();

        // Resolves the current user's permitted site set from their SiteId claims
        // so Deployment/Monitoring pages can enforce site scoping (CentralUI-002).
        services.AddScoped<SiteScopeService>();

        // Centralised dialog service: pages inject IDialogService and a single
        // <DialogHost /> in MainLayout renders the active dialog. See
        // Components/Shared/IDialogService.cs.
        services.AddScoped<IDialogService, DialogService>();

        // Roslyn-backed C# analysis for the Monaco script editor.
        // Scoped because SharedScriptCatalog wraps a scoped service.
        services.AddMemoryCache(o => o.SizeLimit = 200);
        services.AddScoped<ISharedScriptCatalog, SharedScriptCatalog>();
        services.AddScoped<ScriptAnalysisService>();

        return services;
    }
}
