using Kompas.Mcp.Core;
using Kompas.Mcp.Core.Assemblies;
using Kompas.Mcp.Core.Conversion;
using Kompas.Mcp.Core.Documents;
using Kompas.Mcp.Core.Drawings;
using Kompas.Mcp.Core.Editing;
using Kompas.Mcp.Core.Modeling;
using Kompas.Mcp.Core.Query;
using Kompas.Mcp.Core.Threading;
using Kompas.Mcp.Core.Validation;
using Kompas.Mcp.Core.Vision;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

var builder = Host.CreateApplicationBuilder(args);

// ВАЖНО: stdout занят протоколом MCP (stdio). Все логи — только в stderr.
builder.Logging.AddConsole(o => o.LogToStandardErrorThreshold = LogLevel.Trace);
builder.Logging.SetMinimumLevel(LogLevel.Information);

// COM-слой: один STA-поток, одно подключение, общие сервисы (синглтоны).
builder.Services.AddSingleton<KompasDispatcher>();
builder.Services.AddSingleton<KompasSession>();
builder.Services.AddSingleton<DocumentService>();
builder.Services.AddSingleton<PartModeler>();
builder.Services.AddSingleton<HoleService>();
builder.Services.AddSingleton<AssemblyService>();
builder.Services.AddSingleton<DrawingService>();
builder.Services.AddSingleton<VariableService>();
builder.Services.AddSingleton<SnapshotService>();
builder.Services.AddSingleton<QueryService>();
builder.Services.AddSingleton<ModelInspectionService>();
builder.Services.AddSingleton<ConversionService>();
builder.Services.AddSingleton<FaceEditService>();
builder.Services.AddSingleton<SolidEditService>();
builder.Services.AddSingleton<ValidationService>();

// MCP-сервер поверх stdio; инструменты находятся по атрибутам в этой сборке.
builder.Services
    .AddMcpServer()
    .WithStdioServerTransport()
    .WithToolsFromAssembly();

await builder.Build().RunAsync();
