using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Mindforge.API.Providers;
using Mindforge.API.Services;
using Mindforge.API.Services.Interfaces;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllers();
builder.Logging.AddConsole();
builder.Logging.AddDebug();

// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();

// Configure CORS
builder.Services.AddCors(options =>
{
    options.AddPolicy("AllowAll", policy =>
    {
        policy.AllowAnyOrigin()
              .AllowAnyMethod()
              .AllowAnyHeader();
    });
});

// Register HttpClient for providers
builder.Services.AddHttpClient();

// Register Providers
builder.Services.AddScoped<ILlmApiProvider, OpenAIApiProvider>();
builder.Services.AddScoped<ILlmApiProvider, GeminiApiProvider>();

// Register Services
builder.Services.AddScoped<IAgentService, AgentService>();
builder.Services.AddScoped<IFileService, FileService>();
builder.Services.AddScoped<IFlashcardService, FlashcardService>();

var app = builder.Build();

app.UseCors("AllowAll");

app.UseMiddleware<Mindforge.API.Middlewares.ExceptionHandlingMiddleware>();

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

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();

// Check for env vars
var openAiKey = builder.Configuration["OPENAI_API_KEY"];
var geminiKey = builder.Configuration["GEMINI_API_KEY"];

if (string.IsNullOrEmpty(openAiKey))
{
    app.Logger.LogWarning("OPENAI_API_KEY not found in configuration.");
}

if (string.IsNullOrEmpty(geminiKey))
{
    app.Logger.LogWarning("GEMINI_API_KEY not found in configuration.");
}

app.Run();
