using System.Diagnostics;
using Flurl.Http;
using Hellang.Middleware.ProblemDetails;
using InnovEnergy.App.Backend.Database;
using InnovEnergy.App.Backend.Websockets;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Mvc;
using Microsoft.OpenApi.Models;
using InnovEnergy.Lib.Utils;

namespace InnovEnergy.App.Backend;

public static class Program
{
    public static async Task Main(String[] args)
    {
        //First, we initialize the database. This is an empty constructor of the Db class that will be called.
        //In addition, we initialize WatchDog in order to restart the backend service in case of failure.
        //Finally, we start all the  backend services. We call the InitializeEnvironment function of RabbitMqManager to create the queue (factory/connection)
        //Then, we generate a consumer that binds to the queue. This is a separate async Task so it must not be awaited (it acts as a separate thread).
        //Finally, we call the MonitorSalimaxInstallationTable and MonitorSalidomoInstallationTable from the WebsocketManager class.
        //Those methods will build in-memory data structures to track the connected frontends and update them regarding the offline installations.
        
        Watchdog.NotifyReady();
        Db.Init();
        var builder = WebApplication.CreateBuilder(args);
        
        RabbitMqManager.InitializeEnvironment();
        RabbitMqManager.StartRabbitMqConsumer();
        WebsocketManager.MonitorSalimaxInstallationTable();
        WebsocketManager.MonitorSalidomoInstallationTable();
        
    
        builder.Services.AddControllers();
        builder.Services.AddProblemDetails(setup =>
        {
            //This includes the stacktrace in Development Env
            setup.IncludeExceptionDetails = (_, _) => builder.Environment.IsDevelopment() || builder.Environment.IsStaging();
            
            //This handles our Exceptions
            setup.Map<Exceptions>(exception => new ProblemDetails
            {
                Detail = exception.Detail,
                Status = exception.Status,
                Type = exception.Type,
                Instance = exception.Instance
            });
            
        });
        builder.Services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", OpenApiInfo);
            c.UseAllOfToExtendReferenceSchemas();
            c.SupportNonNullableReferenceTypes();
        });

        var app = builder.Build();

        app.Use(async (context, next) =>
        {
            context.Request.WriteLine();
            
            await next(context);
        });

        
        app.UseWebSockets();
        app.UseForwardedHeaders(new ForwardedHeadersOptions
        {
            ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
        });
        
        if (app.Environment.IsDevelopment())
        {
            app.UseSwagger();
            app.UseSwaggerUI();
        }
        
        app.UseCors(p => p.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()) ;
        //app.UseHttpsRedirection();
        app.MapControllers();
        app.UseProblemDetails();
        
        app.Run();
    }

    private static OpenApiInfo OpenApiInfo { get; } = new OpenApiInfo
    {
        Title = "Innesco Backend API",
        Version = "v1"
    };

}