using MilkStream;
using MilkStream.Components;
using MilkStream.Services;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Configuration.AddJsonFile("appsettings.json", optional: false)
    .AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", true, true);
builder.Services.AddRazorComponents().AddInteractiveServerComponents();
builder.Services.AddHttpClient();
builder.Services.AddSession();
#if DEBUG // Enable the Sass compiler in debug mode for hot reload support.
builder.Services.AddSassCompiler();
#endif

//TODO: this probably needs a better system for the settings.
builder.Services.AddScoped<LoginService>();
builder.Services.Configure<LoginServiceOptions>(options => { options.BaseUrl = builder.Configuration["BaseUrl"]!; });
builder.Services.AddScoped<MediaService>();
builder.Services.Configure<MediaServiceOptions>(options => { options.BaseUrl = builder.Configuration["BaseUrl"]!; });
builder.Services.AddScoped<SettingsService>();
builder.Services.Configure<SettingsServiceOptions>(options => { options.BaseUrl = builder.Configuration["BaseUrl"]!; });
builder.Services.AddScoped<UserService>();
builder.Services.Configure<UserServiceOptions>(options => { options.BaseUrl = builder.Configuration["BaseUrl"]!; });
//http listener for all requests with automatic JWT token refresh on 401 Unauthorized responses.
builder.Services.AddTransient<JwtTokenRefresher>();
builder.Services.AddHttpClient("MilkstreamClient").AddHttpMessageHandler<JwtTokenRefresher>();
builder.WebHost.UseStaticWebAssets();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment()) {
    app.UseExceptionHandler("/Error", createScopeForErrors: true);
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAntiforgery();
app.MapRazorComponents<App>().AddInteractiveServerRenderMode();

app.Run();
