using Autofac;
using PlanTempus.Application.Features.Localization.Services;
using PlanTempus.Application.Features.Menu.Services;
using PlanTempus.Components.ModuleRegistry;
using PlanTempus.Components.Outbox;
using PlanTempus.Core.Configurations.JsonConfigProvider;
using PlanTempus.Core.Configurations;
using PlanTempus.Core.Email;
using PlanTempus.Core.ModuleRegistry;
using PlanTempus.Core.Outbox;

namespace PlanTempus.Application;

public class Startup
{
    public Startup(IWebHostEnvironment env)
    {
        var builder = new Core.Configurations.ConfigurationBuilder()
            .AddJsonFile("appconfiguration.json", optional: true, reloadOnChange: true);

        ConfigurationRoot = builder.Build();
    }

    public Core.Configurations.IConfigurationRoot ConfigurationRoot { get; private set; }

    public ILifetimeScope? AutofacContainer { get; private set; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        services.AddOptions();

        // Add Razor Pages with feature-based structure
        services.AddRazorPages(options =>
        {
            options.RootDirectory = "/Features";
        })
        .AddRazorOptions(options =>
        {
            options.ViewLocationFormats.Add("/Features/_Shared/Pages/{0}.cshtml");
            options.ViewLocationFormats.Add("/Features/_Shared/Components/{1}/{0}.cshtml");
        });

        services.AddAntiforgery(x => x.HeaderName = "X-ANTI-FORGERY-TOKEN");
    }

    public void ConfigureContainer(ContainerBuilder builder)
    {
        // Application services
        builder.RegisterType<MockMenuService>().As<IMenuService>().InstancePerLifetimeScope();
        builder.RegisterType<JsonLocalizationService>().As<ILocalizationService>().InstancePerLifetimeScope();

        // Infrastructure modules
        builder.RegisterModule(new Database.ModuleRegistry.DbPostgreSqlModule
        {
            ConnectionString = ConfigurationRoot.GetConnectionString("DefaultConnection")
        });

        builder.RegisterModule(new TelemetryModule
        {
            TelemetryConfig = ConfigurationRoot.GetSection("ApplicationInsights").ToObject<TelemetryConfig>()
        });

        builder.RegisterModule<CommandModule>();
        builder.RegisterModule<OutboxModule>();
        builder.RegisterModule<OutboxListenerModule>();
        builder.RegisterModule(new EmailModule
        {
            PostmarkConfiguration = ConfigurationRoot.GetSection("Postmark").ToObject<PostmarkConfiguration>()
        });
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
            app.UseDeveloperExceptionPage();
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }

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

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapRazorPages();
            endpoints.MapControllers();
        });
    }
}
