using Flyshot.Core.Config;
using Flyshot.Runtime.Common;
using Flyshot.Runtime.Fanuc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace Flyshot.ControllerClientCompat;

/// <summary>
/// 提供 ControllerClient HTTP 兼容层的依赖注入注册入口。
/// </summary>
public static class ControllerClientCompatServiceCollectionExtensions
{
    /// <summary>
    /// 将 HTTP-only 的 ControllerClient 兼容服务注册到当前宿主。
    /// </summary>
    /// <param name="services">当前宿主服务集合。</param>
    /// <param name="configuration">宿主配置根。</param>
    /// <returns>同一服务集合，便于链式调用。</returns>
    public static IServiceCollection AddControllerClientCompat(this IServiceCollection services, IConfiguration configuration)
    {
        ArgumentNullException.ThrowIfNull(services);
        ArgumentNullException.ThrowIfNull(configuration);

        services
            .AddOptions<ControllerClientCompatOptions>()
            .Bind(configuration.GetSection("ControllerClientCompat"));

        services.AddSingleton(static serviceProvider => serviceProvider.GetRequiredService<IOptions<ControllerClientCompatOptions>>().Value);
        services.AddSingleton<RobotModelLoader>();
        services.AddSingleton<RobotConfigLoader>();
        services.AddSingleton<ControllerClientCompatRobotCatalog>();
        services.AddSingleton<ControllerClientTrajectoryOrchestrator>();
        services.AddSingleton<FlyshotTrajectoryArtifactWriter>();
        services.AddSingleton<JsonFlyshotTrajectoryStore>();
        services.AddSingleton<IControllerRuntime, FanucControllerRuntime>();
        services.AddSingleton<IControllerClientCompatService, ControllerClientCompatService>();

        return services;
    }
}
