using Intentor.Components;
using Intentor.Services;

namespace Intentor;

public class Program
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);

        // Get Home Assistant Supervisor token
        var supervisorToken = Environment.GetEnvironmentVariable("SUPERVISOR_TOKEN")
            ?? throw new InvalidOperationException("SUPERVISOR_TOKEN environment variable is required");

        // Add services to the container.
        builder.Services.AddHttpClient<HomeAssistantService>(client =>
        {
            client.DefaultRequestHeaders.Add("Authorization", $"Bearer {supervisorToken}");
        });
        builder.Services.AddSingleton(sp =>
            new HomeAssistantService(sp.GetRequiredService<IHttpClientFactory>().CreateClient(nameof(HomeAssistantService)), supervisorToken));

        builder.Services.AddRazorComponents()
            .AddInteractiveServerComponents();

        var app = builder.Build();

        // 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.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true);
        app.UseHttpsRedirection();

        app.UseAntiforgery();

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

        app.Run();
    }
}