using Exsersewo.Common.Utilities;
using Kynareth.Managers;
using Kynareth.Models;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseKestrel();

HttpWebClient.UserAgent = "IBrowse/2.4 (AmigaOS 3.9; 68K)";

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services
    .AddEndpointsApiExplorer()
    .AddSwaggerGen();
    
builder.Services
    .AddAuthentication();

builder.Services.AddDbContext<KynarethDbContext>(options => 
{
    var connStr = builder.Configuration.GetConnectionString("Database");

    var serverVersion = ServerVersion.AutoDetect(connStr);

    options.UseMySql(connStr, serverVersion, x => x.EnableRetryOnFailure());
    options.UseLazyLoadingProxies();
}, contextLifetime: ServiceLifetime.Transient, optionsLifetime: ServiceLifetime.Singleton);

var app = builder.Build();

ImageManager.Configure(app.Configuration, app.Environment);

// Configure the HTTP request pipeline.
//if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseAuthorization();

app.MapControllers();

app.Services.GetService<ILogger<Program>>().LogInformation($"Ready and waiting requests @ {string.Join(";", app.Urls)}");

app.Run();