using TradingBot.Components;
using TradingBot.Services;

var builder = WebApplication.CreateBuilder(args);

// Configure Kestrel - Solo in Development usa porte da launchSettings
// In Production/Docker usa porta 8080 su tutte le interfacce
if (builder.Environment.IsProduction() || builder.Environment.EnvironmentName == "Docker")
{
    builder.WebHost.ConfigureKestrel(serverOptions =>
    {
        serverOptions.ListenAnyIP(8080);
    });
}

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

// Trading Bot Services - Using Simulated Market Data
builder.Services.AddSingleton<IMarketDataService, SimulatedMarketDataService>();
builder.Services.AddSingleton<ITradingStrategy, SimpleMovingAverageStrategy>();
builder.Services.AddSingleton<TradeHistoryService>();
builder.Services.AddSingleton<LoggingService>();
builder.Services.AddSingleton<TradingBotService>();
builder.Services.AddSingleton<SettingsService>();

// Register background service for graceful shutdown
builder.Services.AddHostedService<TradingBotBackgroundService>();

// Add health checks for Docker
builder.Services.AddHealthChecks();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error", createScopeForErrors: true);
    app.UseHsts();
}

// HTTPS redirect solo in Development
if (app.Environment.IsDevelopment())
{
    app.UseHttpsRedirection();
}

app.UseStaticFiles();
app.UseAntiforgery();

// Health check endpoint
app.MapHealthChecks("/health");

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

app.Run();
