using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi;
using SQLiteEF;

namespace API;

public class Api
{
    private Tracker _tracker;
    public Api(string[] args)
    {
        WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

        // Add services to the container.

        builder.Services.AddControllers().AddNewtonsoftJson();
        // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
        builder.Services.AddOpenApi(options =>
        {
            options.OpenApiVersion = OpenApiSpecVersion.OpenApi3_0;
        });
        builder.Services.AddCors(options => options.AddPolicy("AllowAll", p => p.AllowAnyOrigin().AllowAnyMethod()));
        
        builder.Services.AddSwaggerGen();

        builder.Services.AddDbContext<Context>(options => options.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection"), optionsBuilder => optionsBuilder.MigrationsAssembly(Assembly.GetAssembly(GetType())!)));

        builder.Services.AddSingleton<Tracker>();
        SteamApiWrapper.SteamApiWrapper.SetApiKey(builder.Configuration.GetValue<string>("SteamWebApiKey")!);

        WebApplication app = builder.Build();

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

        app.UseHttpsRedirection();

        app.UseAuthorization();
        
        app.MapControllers();
        
        app.Services.CreateScope().ServiceProvider.GetRequiredService<Context>().Database.Migrate();

        app.Run();
    }
    
    public static void Main(string[] args)
    {
        Api _ = new (args);
    }
}