using System.Text;
using Common.Global;
using Database.Database;
using Esim.Apis.Business;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;

var builder = WebApplication.CreateBuilder(args);

// Set global configuration for use by singletons like ConfigManager
GlobalConfig.Configuration = builder.Configuration;

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

// Add DbContext with Oracle provider
var connectionString = builder.Configuration.GetSection("Connection").Value;
builder.Services.AddDbContext<ModelContext>(options =>
    options.UseOracle(connectionString));

// Add Business Services
builder.Services.AddScoped<IUserBusiness, UserBusinessImpl>();
builder.Services.AddScoped<IArticleBusiness, ArticleBusinessImpl>();
builder.Services.AddScoped<IContentBusiness, ContentBusinessImpl>();

// Configure CORS - Allow frontend to access APIs
builder.Services.AddCors(options =>
{
    options.AddPolicy("AllowFrontend", policy =>
    {
        policy.WithOrigins(
                "http://localhost:3000",      // React development
                "http://localhost:5173",      // Vite development
                "http://localhost:4200",      // Angular development
                "https://infigate.vn",        // Production domain
                "https://www.infigate.vn"     // Production www domain
            )
            .AllowAnyMethod()
            .AllowAnyHeader()
            .AllowCredentials();
    });
});

// Configure JWT Authentication
var jwtKey = builder.Configuration["Jwt:Key"] ?? "EsimLaoSecretKey12345678901234567890";
var jwtIssuer = builder.Configuration["Jwt:Issuer"] ?? "EsimLao";
var jwtAudience = builder.Configuration["Jwt:Audience"] ?? "EsimLaoClient";

builder.Services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
    options.TokenValidationParameters = new TokenValidationParameters
    {
        ValidateIssuer = true,
        ValidateAudience = true,
        ValidateLifetime = true,
        ValidateIssuerSigningKey = true,
        ValidIssuer = jwtIssuer,
        ValidAudience = jwtAudience,
        IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtKey))
    };
});

// Add Swagger for API documentation
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Initialize ConfigManager to load configs from database
using (var scope = app.Services.CreateScope())
{
    var logger = scope.ServiceProvider.GetRequiredService<ILogger<Program>>();
    try
    {
        logger.LogInformation("Initializing ConfigManager...");
        Esim.Apis.Singleton.ConfigManager.Instance.Initialize();
        logger.LogInformation("ConfigManager initialized successfully");
        
        // Start background refresh task
        Task.Run(() => Esim.Apis.Singleton.ConfigManager.Instance.RefreshConfigs());
    }
    catch (Exception ex)
    {
        logger.LogError(ex, "Failed to initialize ConfigManager");
    }
}
app.UseSwagger();
app.UseSwaggerUI();
// Configure the HTTP request pipeline.
//if (app.Environment.IsDevelopment())
//{
//    app.UseSwagger();
//    app.UseSwaggerUI();
//}
//else
//{
//    app.UseExceptionHandler("/Home/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.UseHttpsRedirection();
app.UseRouting();

// Enable CORS - MUST be after UseRouting and before UseAuthentication
app.UseCors("AllowFrontend");

app.UseAuthentication();
app.UseAuthorization();

app.MapStaticAssets();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}")
    .WithStaticAssets();

app.MapControllers();

app.Run();

