using Microsoft.EntityFrameworkCore;
using Radzen;
using TestApp.Components;
using TestApp.Models;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorComponents()
    .AddInteractiveServerComponents();

builder.Services.AddDbContext<DatabaseContext>(options =>
{
    options.UseSqlite("Data Source=Test.db")
    .EnableSensitiveDataLogging().UseSeeding((context, _) =>
    {
        if (context.Set<Testentity>().Any())
        {
            return; // Daten sind bereits vorhanden, kein Seeding erforderlich
        }
        for (int i = 1; i < 100; i++)
        {
            context.Set<Testentity>().Add(new Testentity { Id = i, Name = $"Name {i}", Description = $"Description {i}" });
        }
        context.SaveChanges();
    });
});

builder.Services.AddRadzenComponents();

var app = builder.Build();

// Datenbank erstellen und Seed-Daten laden
using (var scope = app.Services.CreateScope())
{
    var db = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
    db.Database.EnsureCreated();
}

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error", createScopeForErrors: true);
}
app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true);
app.UseAntiforgery();

app.MapStaticAssets();
app.MapRazorComponents<App>()
    .AddInteractiveServerRenderMode();

app.Run();
