using Application.Abstractions.Authentication;
using Application.Abstractions.Cache;
using Application.Abstractions.Chat;
using Application.Abstractions.Crypto;
using Application.Abstractions.Data;
using Application.Abstractions.Identity;
using Application.Abstractions.Forum;
using Application.Abstractions.Messaging.Email;
using Infrastructure.Authentication;
using Infrastructure.Forum;
using Infrastructure.Cache;
using Infrastructure.Chat;
using Infrastructure.Crypto;
using Infrastructure.Messaging.Email;
using Infrastructure.News;
using Infrastructure.Persistence;
using Infrastructure.Persistence.Identity;
using Infrastructure.Storage;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using SharedKernel;
using SharedKernel.Storage;
using StackExchange.Redis;
using System.Text;

namespace Infrastructure
{
    public static class DependencyInjection
    {
        // SQL Server
        private static IServiceCollection AddDatabase(this IServiceCollection services, IConfiguration configuration)
        {
            var dbConn = configuration.GetConnectionString("DefaultConnection");
            if (string.IsNullOrWhiteSpace(dbConn))
            {
                throw new InvalidOperationException("Connection string 'DefaultConnection' is not configured.");
            }

            services.AddDbContext<AppDbContext>(options => options.UseSqlServer(dbConn));
            services.AddDbContext<IdentityDbContext>(options => options.UseSqlServer(dbConn));

            services.AddScoped<IAppDbContext>(sp => sp.GetRequiredService<AppDbContext>());

            return services;
        }

        // Redis Server
        public static IServiceCollection AddRedis(this IServiceCollection services, IConfiguration configuration)
        {
            var settings = configuration.Get<AppSettings>()!;
            var redis = ConnectionMultiplexer.Connect(settings.Redis.DefaultConnection);

            services.AddSingleton<IConnectionMultiplexer>(redis);
            services.AddDataProtection().SetApplicationName(settings.App.Name).PersistKeysToStackExchangeRedis(redis, settings.Redis.DataProtectionKey).SetDefaultKeyLifetime(settings.Redis.DefaultKeyLifetime); // 기본 90일 주기

            // Distributed Cache 설정
            services.AddStackExchangeRedisCache(options =>
            {
                options.Configuration = settings.Redis.DefaultConnection;
                options.InstanceName = settings.Redis.CachePrefix;
            });

            return services;
        }

        private static IServiceCollection AddHealthChecks(this IServiceCollection services, IConfiguration configuration)
        {
            var settings = configuration.Get<AppSettings>()!;

            services.AddHealthChecks().AddSqlServer(settings.ConnectionStrings.DefaultConnection).AddRedis(settings.Redis.DefaultConnection);

            return services;
        }

        private static IServiceCollection AddServices(this IServiceCollection services)
        {
            services.AddSingleton<IJwtTokenProvider, JwtTokenProvider>();
            services.AddSingleton<ICacheService, RedisCacheService>();
            services.AddSingleton<IFieldEncryptor, AesGcmFieldEncryptor>();
            services.AddTransient<IMailService, MailService>();
            services.AddTransient<IEmailSender, IdentityEmailSender>();
            services.AddScoped<IFileStorage, LocalFileStorage>();
            services.AddScoped<IEditorImageService, EditorImageService>();
            services.AddScoped<IIdentityUserReader, IdentityUserReader>();
            services.AddScoped<IIdentityUserWriter, IdentityUserWriter>();
            services.AddScoped<IIdentityRoleReader, IdentityRoleReader>();
            services.AddScoped<IIdentityRoleWriter, IdentityRoleWriter>();
            services.AddHttpClient<IUpbitClient, UpbitRestClient>(client =>
            {
                client.BaseAddress = new Uri("https://api.upbit.com/v1/");
                client.DefaultRequestHeaders.Add("Accept", "application/json");
                client.Timeout = TimeSpan.FromSeconds(10);
            });
            services.AddSingleton<IChatMessageStore, RedisChatMessageStore>();
            services.AddSingleton<IChatConnectionTracker, RedisChatConnectionTracker>();
            services.AddScoped<IBoardPermissionService, BoardPermissionService>();
            services.AddHttpClient<IGoogleTokenValidator, GoogleTokenValidator>();
            services.AddHttpClient("RssFeed", client =>
            {
                client.Timeout = TimeSpan.FromSeconds(30);
                client.DefaultRequestHeaders.Add("User-Agent", "BitForum/1.0");
                client.DefaultRequestHeaders.Add("Accept", "application/rss+xml, application/xml, text/xml");
            });

            return services;
        }

        /**
         * ========================================================================================================================================================================================================
         * ========================================================================================================================================================================================================
         */

        // Admin 전용
        public static IServiceCollection AddAdminInfrastructure(this IServiceCollection services, IConfiguration configuration)
        {
            return services.AddDatabase(configuration).AddRedis(configuration).AddServices().AddNews().AddHealthChecks(configuration);
        }

        /**
         * ========================================================================================================================================================================================================
         * ========================================================================================================================================================================================================
         */

        // API 전용
        public static IServiceCollection AddApiInfrastructure(this IServiceCollection services, IConfiguration configuration)
        {
            return services.AddDatabase(configuration).AddRedis(configuration).AddApiAuthentication(configuration).AddServices().AddUpbit().AddNews().AddHealthChecks(configuration);
        }

        private static IServiceCollection AddUpbit(this IServiceCollection services)
        {
            services.AddHostedService<UpbitWebSocketService>();

            return services;
        }

        private static IServiceCollection AddNews(this IServiceCollection services)
        {
            services.AddSingleton<RssCollectorService>();
            services.AddHostedService(sp => sp.GetRequiredService<RssCollectorService>());
            services.AddSingleton<Application.Abstractions.News.IRssCollector>(sp => sp.GetRequiredService<RssCollectorService>());

            return services;
        }

        private static IServiceCollection AddApiAuthentication(this IServiceCollection services, IConfiguration configuration)
        {
            var settings = configuration.Get<AppSettings>()!;

            // 인증 정책- JWT Bearer
            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(options =>
            {
                options.RequireHttpsMetadata = false;
                options.MapInboundClaims = false;
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer = true,
                    ValidateAudience = true,
                    ValidateLifetime = true,
                    ValidateIssuerSigningKey = true,
                    ValidIssuer = settings.JWT.Issuer,
                    ValidAudience = settings.JWT.Audience,
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(settings.JWT.SecretKey)),
                    ClockSkew = TimeSpan.Zero
                };

                options.Events = new JwtBearerEvents
                {
                    OnMessageReceived = context =>
                    {
                        var accessToken = context.Request.Query["access_token"];
                        var path = context.HttpContext.Request.Path;

                        if (!string.IsNullOrEmpty(accessToken) && path.StartsWithSegments("/hubs"))
                        {
                            context.Token = accessToken;
                        }

                        return Task.CompletedTask;
                    }
                };
            });

            services.AddAuthorization();

            // Identity Core (Admin Handler의 UserManager/RoleManager 의존 해소용)
            services.AddIdentityCore<ApplicationUser>().AddRoles<IdentityRole>().AddEntityFrameworkStores<IdentityDbContext>();

            return services;
        }
    }
}