using ConfigurationPannel.Services;
using Microsoft.AspNetCore.Authentication.Cookies;
using ConfigurationManager = ConfigurationPannel.Services.ConfigurationManager;

var builder = WebApplication.CreateBuilder(args);

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

// Authentication
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie(options =>
    {
        options.LoginPath = "/Login";
        options.LogoutPath = "/Logout";
        options.ExpireTimeSpan = TimeSpan.FromHours(8);
        options.SlidingExpiration = true;
    });
builder.Services.AddAuthorization();

// Application services
builder.Services.AddSingleton<UserService>();
builder.Services.AddSingleton<ConfigurationManager>();
builder.Services.AddSingleton<LogoService>();

// PrintServer as hosted service
builder.Services.AddHostedService<PrintServerHostedService>();

// Make PrintServerHostedService available for injection (for restart capability)
builder.Services.AddSingleton(sp =>
    sp.GetServices<IHostedService>()
      .OfType<PrintServerHostedService>()
      .FirstOrDefault()!);

var app = builder.Build();

// set port
app.Urls.Add($"http://*:80");

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

app.UseStaticFiles();
app.UseRouting();

// Authentication & Authorization
app.UseAuthentication();
app.UseAuthorization();

app.MapRazorPages();

app.Run();
