using Serilog;
using Tiku.Application;
using Tiku.Infrastructure;

namespace Tiku.Api.Configuration;

public static class DependencyInjection
{
    public static WebApplicationBuilder AddApiServices(this WebApplicationBuilder builder)
    {
        builder.Services.AddSerilog((services, configuration) => configuration
            .ReadFrom.Configuration(builder.Configuration)
            .ReadFrom.Services(services)
            .Enrich.FromLogContext(),
            preserveStaticLogger: true);

        builder.Services.AddApiPresentation();
        builder.Services.AddApplication();
        builder.Services.AddNetworkConfiguration(builder.Configuration);
        builder.Services.AddApiRateLimiting(builder.Configuration);

        var connectionString = Options.OptionsValidation.ResolveDatabaseConnectionString(
            builder.Configuration,
            builder.Environment.IsDevelopment());
        builder.Services.AddInfrastructure(connectionString);

        builder.Services.AddApiDataProtection(builder.Configuration, builder.Environment);
        builder.Services.AddExternalServiceOptions(builder.Configuration, builder.Environment);
        builder.Services.AddApiAuthenticationAndAuthorization(builder.Configuration, builder.Environment);

        return builder;
    }
}
