using DataLayer;
using DataLayer.Interfaces;
using Microsoft.EntityFrameworkCore;
using Services;
using Services.Interfaces;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();

builder.Services.AddDbContext<DeathwatchDbContext>(options =>
    options.UseInMemoryDatabase("InMemoryDb"));

builder.Services.AddTransient(typeof(IAbstractRepository<>), typeof(AbstractRepository<>));
builder.Services.AddTransient<IPlayerService, PlayerService>();
builder.Services.AddTransient<IPlayerRepository, PlayerRepository>();

builder.Services.AddHttpClient();
//builder.Services.AddSqlite<DeathwatchDbContext>("Data Source=deathwatch.db");

var app = builder.Build();

var scopeFactory = app.Services.GetRequiredService<IServiceScopeFactory>();
using (var scope = scopeFactory.CreateScope())
{
    //var parent = Directory.GetParent(Directory.GetCurrentDirectory()).Parent;
    var directory = Directory.GetCurrentDirectory();
    //var folderPath = Path.Combine($"{directory}", "Dotnet", "Deathwatch", "XML");
    var folderPath = Path.Combine($"{directory}", "XML");
    var db = scope.ServiceProvider.GetRequiredService<DeathwatchDbContext>();
    if (db.Database.EnsureCreated())
    {
        DbContextInitializer.Initialize(db, folderPath);
    }
}

// Configure the HTTP request pipeline.
//if (!app.Environment.IsDevelopment())
//{
app.UseExceptionHandler("/Error");
// 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.UseRouting();

app.MapBlazorHub();
app.MapFallbackToPage("/_Host");

app.Run();

