using ASTRAIN.Api.Data;
using ASTRAIN.Api.Endpoints;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddOpenApi();
builder.Services.AddCors(options =>
{
    options.AddDefaultPolicy(policy =>
    {
        policy.AllowAnyOrigin()
            .AllowAnyHeader()
            .AllowAnyMethod();

        // policy.WithOrigins("http://localhost:5016", "http://:5016", "https://localhost:7252")
        //     .AllowAnyHeader()
        //     .AllowAnyMethod();
    });
});
builder.Services.AddDbContext<AppDbContext>(options =>
{
    var connectionString = builder.Configuration.GetConnectionString("Default")
                           ?? "Data Source=Data/astrain.db";
    options.UseSqlite(connectionString);
});

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.MapOpenApi();
}

// app.UseHttpsRedirection();
app.UseCors();
app.UseDefaultFiles();
app.UseStaticFiles();

using (var scope = app.Services.CreateScope())
{
    var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
    db.Database.EnsureCreated();
}

var api = app.MapGroup("/api");

api.MapHealthEndpoints();
api.MapUserEndpoints();
api.MapExerciseEndpoints();
api.MapRoutineEndpoints();
api.MapRoutineRunEndpoints();

app.MapFallbackToFile("index.html");

app.Run();
