using Insight.Api.Hosting;
using Insight.Domain.Constants;
using Insight.Infrastructure;
using Microsoft.Extensions.FileProviders;

namespace Insight.Api
{
    public class Program
    {
        public static async Task Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);
            builder.Host.UseWindowsService();
            builder.Host.UseSystemd();

            // LOGGER
            builder.Logging.ClearProviders();
            builder.Logging.SetMinimumLevel(LogLevel.Trace);
            builder.Logging.AddFilter("Microsoft.AspNetCore", LogLevel.Warning);

            builder.Logging.AddSimpleConsole(options =>
            {
                options.IncludeScopes = true;
                options.SingleLine = true;
                options.TimestampFormat = "yyyy-MM-dd HH:mm:ss.fff ";
            });

            builder.Logging.AddFile($"{Configuration.AppDirectory?.FullName}/" + "logs/api_{Date}.log", LogLevel.Trace, fileSizeLimitBytes: 104857600, retainedFileCountLimit: 10, outputTemplate: "{Timestamp:o} [{Level:u3}] {Message} {NewLine}{Exception}");

            // INFRASTRUCTURE
            builder.Services.AddDatabase(builder.Configuration);
            builder.Services.AddInfrastructureServices();

            // IDENTITY
            builder.Services.AddIdentityServices(builder.Configuration);
            builder.Services.AddBearerAuthentication(builder.Configuration);
            builder.Services.AddTokenServices(builder.Configuration);

            // SECURITY
            builder.Services.AddAuthorization();

            // WEBSERVICES
            builder.Services.AddProxyServices(builder.Configuration);
            builder.Services.AddRoutingServices(builder.Configuration);
            builder.Services.AddControllers();

            // SWAGGER
            builder.Services.AddSwaggerServices(builder.Configuration);

            //builder.Services.AddControllers();
            //builder.Services.AddEndpointsApiExplorer();
            //builder.Services.AddSwaggerGen();

            var app = builder.Build();

            // Configure the HTTP request pipeline.
            app.UseForwardedHeaders();

            if (app.Environment.IsDevelopment())
            {

            }

            app.UseSwagger(options =>
            {
                options.RouteTemplate = "api/swagger/{documentName}/swagger.json";
            });

            app.UseSwaggerUI(options =>
            {
                options.DefaultModelsExpandDepth(-1);
                options.SwaggerEndpoint("/api/swagger/v1/swagger.json", "v1");
                options.RoutePrefix = "api/swagger";
            });

            app.UseCors(x => x
                .AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader());

            app.UseAuthorization();

            app.MapControllers();

            // STATIC FILES (UPDATES)
            var files = new DirectoryInfo($"{Domain.Constants.Configuration.AppDirectory?.FullName}/files");
            files.Create();

            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(files.FullName),
                RequestPath = "/api/files",
                ServeUnknownFileTypes = true
            });

            await app.RunAsync();
        }
    }
}