using Elastic.CommonSchema.Serilog;
using PARR.Core;
using PARR.DAL;
using PARR.Infrastructure;
using PARR.TaskReconciliationWorker.Settings;
using Serilog;

var builder = Host.CreateApplicationBuilder();

builder.Services.AddLogging(config =>
{
    config.ClearProviders();

    var logger = new LoggerConfiguration();

    if (builder.Environment.IsProduction())
        logger.WriteTo.Console(new EcsTextFormatter());
    else
        logger.WriteTo.Console();

    logger.ReadFrom.Configuration(builder.Configuration);

    config.AddSerilog(logger.CreateLogger());
});

// --- Настройки ---
var workerSettings = new WorkerSettings();
builder.Configuration.GetSection(nameof(WorkerSettings)).Bind(workerSettings);

builder.Services.AddDalServices(builder.Configuration);
builder.Services.AddCoreServices(builder.Configuration);
builder.Services.AddInfrastructureServices(builder.Configuration);
builder.Configuration.AddDalConfigurations(builder.Services);
builder.Services.AddDallSettings(builder.Configuration);

builder.Services.AddTaskManagement((provider, taskType) =>
{
    // настройки очередей
    //var workerSettings = provider.GetRequiredService<WorkerSettings>();
    if (workerSettings.MqSettings.Tasks.TryGetValue(taskType, out var settings))
        return settings;

    throw new ArgumentException($"Настройки для типа задания {taskType} не найдены в конфигурации.");
});

builder.Services.AddTaskReconciliation(workerSettings);

var host = builder.Build();
host.Run();