using System.Text; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; using Ventas.Infrastructure.Data; using Ventas.Infrastructure.Repositories; using Ventas.Core.Interfaces; using Ventas.Services; var builder = WebApplication.CreateBuilder(args); var connectionString = builder.Configuration.GetConnectionString("Default")!; builder.Services.AddControllers(); builder.Services.AddOpenApi(); builder.Services.AddDbContext(options => options.UseNpgsql(connectionString)); builder.Services.AddScoped(sp => new LeadRepository(connectionString)); builder.Services.AddScoped(sp => new LeadQueryRepository(connectionString)); builder.Services.AddScoped(sp => new ContratoRepository(connectionString)); builder.Services.AddScoped(sp => new CotizacionRepository(connectionString)); builder.Services.AddScoped(sp => new AlumnoRepository(connectionString)); builder.Services.AddScoped(sp => new UsuarioRepository(connectionString)); builder.Services.AddScoped(sp => new InformeRepository(connectionString)); builder.Services.AddScoped(sp => new LeadService(connectionString)); builder.Services.AddScoped(sp => new UsuarioService(sp.GetRequiredService(), connectionString)); builder.Services.AddScoped(sp => new ContratoService(sp.GetRequiredService(), connectionString)); builder.Services.AddScoped(sp => new CotizacionService(sp.GetRequiredService(), connectionString)); builder.Services.AddScoped(sp => new AlumnoService(sp.GetRequiredService(), connectionString)); builder.Services.AddScoped(sp => new ArqueoService(connectionString)); builder.Services.AddScoped(); builder.Services.AddScoped(sp => new ReportService(sp.GetRequiredService(), sp.GetRequiredService(), connectionString)); builder.Services.AddScoped(sp => new EmpresaReportService(sp.GetRequiredService(), connectionString)); var jwtSecret = builder.Configuration["Jwt:Secret"] ?? "default-dev-secret-change-in-production"; var jwtExpiration = int.Parse(builder.Configuration["Jwt:ExpirationMinutes"] ?? "30"); builder.Services.AddScoped(sp => new JwtService(jwtSecret, jwtExpiration)); builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = false, ValidateAudience = false, ValidateLifetime = true, ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSecret)) }; }); builder.Services.AddCors(options => { options.AddDefaultPolicy(policy => { policy.AllowAnyOrigin() .AllowAnyHeader() .AllowAnyMethod(); }); }); var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.MapOpenApi(); } app.UseCors(); app.UseAuthentication(); app.UseAuthorization(); app.MapControllers(); app.Run();