using Dapper; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using SCM.Application.Authentication; using SCM.Application.Common; using SCM.Domain.Cadastros; using SCM.Domain.Consultas; using SCM.Domain.Contratos; using SCM.Domain.Controle; using SCM.Domain.Materiais; using SCM.Domain.Medicao; using SCM.Domain.Obras; using SCM.Domain.Planejamento; using SCM.Domain.Projects; using SCM.Domain.Qualidade; using SCM.Domain.Qualificacoes; using SCM.Domain.Users; using SCM.Infrastructure.HealthChecks; using SCM.Infrastructure.Persistence; using SCM.Infrastructure.Persistence.Repositories; using SCM.Infrastructure.Security; namespace SCM.Infrastructure; public static class DependencyInjection { public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration) { var connectionString = configuration.GetConnectionString("Postgres") ?? throw new InvalidOperationException("ConnectionStrings:Postgres não configurada."); // Mapeia colunas snake_case do PostgreSQL para propriedades PascalCase (ex.: start_date -> StartDate). DefaultTypeMap.MatchNamesWithUnderscores = true; // Suporte a DateOnly como parâmetro/coluna (Dapper atual não trata nativamente). SqlMapper.AddTypeHandler(new DateOnlyTypeHandler()); // Simplifica o tratamento de timestamp (sem distinção estrita de Kind UTC/local no Npgsql). // TODO: revisar para usar timestamptz + UTC de forma explícita quando o modelo amadurecer. AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); services.AddSingleton(new NpgsqlConnectionFactory(connectionString)); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddSingleton(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.Configure(configuration.GetSection(JwtOptions.SectionName)); services.AddSingleton(); services.AddSingleton(); services.AddHealthChecks() .AddCheck("postgres", tags: new[] { "db", "critical" }); return services; } }