using Alipay.EasySDK.Factory;
using Alipay.EasySDK.Kernel;
using Dm.util;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Domain;
using Yi.Framework.AiHub.Domain.AiGateWay;
using Yi.Framework.AiHub.Domain.AiGateWay.Impl.ThorAzureDatabricks.Chats;
using Yi.Framework.AiHub.Domain.AiGateWay.Impl.ThorAzureOpenAI.Chats;
using Yi.Framework.AiHub.Domain.AiGateWay.Impl.ThorAzureOpenAI.Images;
using Yi.Framework.AiHub.Domain.AiGateWay.Impl.ThorClaude.Chats;
using Yi.Framework.AiHub.Domain.AiGateWay.Impl.ThorCustomOpenAI.Chats;
using Yi.Framework.AiHub.Domain.AiGateWay.Impl.ThorDeepSeek.Chats;
using Yi.Framework.AiHub.Domain.AiGateWay.Impl.ThorSiliconFlow.Embeddings;
using Yi.Framework.AiHub.Domain.Managers.Fuwuhao;
using Yi.Framework.AiHub.Domain.Shared;
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
using Yi.Framework.Mapster;
using Microsoft.Extensions.DependencyInjection;
using Yi.Framework.AiHub.Domain.AiGateWay.Impl.ThorGemini.Chats;

namespace Yi.Framework.AiHub.Domain
{
    [DependsOn(
        typeof(YiFrameworkAiHubDomainSharedModule),
        typeof(YiFrameworkMapsterModule),
        typeof(AbpDddDomainModule)
    )]
    public class YiFrameworkAiHubDomainModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var configuration = context.Services.GetConfiguration();
            var services = context.Services;

            #region OpenAi ChatCompletion

            services.AddKeyedTransient<IChatCompletionService, AzureOpenAiChatCompletionCompletionsService>(
                nameof(AzureOpenAiChatCompletionCompletionsService));
            services.AddKeyedTransient<IChatCompletionService, AzureDatabricksChatCompletionsService>(
                nameof(AzureDatabricksChatCompletionsService));
            services.AddKeyedTransient<IChatCompletionService, DeepSeekChatCompletionsService>(
                nameof(DeepSeekChatCompletionsService));
            services.AddKeyedTransient<IChatCompletionService, OpenAiChatCompletionsService>(
                nameof(OpenAiChatCompletionsService));
            services.AddKeyedTransient<IChatCompletionService, ClaudiaChatCompletionsService>(
                nameof(ClaudiaChatCompletionsService));

            #endregion

            #region Anthropic ChatCompletion
            
            services.AddKeyedTransient<IAnthropicChatCompletionService, AnthropicChatCompletionsService>(
                nameof(AnthropicChatCompletionsService));

            #endregion

            #region OpenAi Response

            services.AddKeyedTransient<IOpenAiResponseService, OpenAiResponseService>(
                nameof(OpenAiResponseService));

            #endregion

            #region Gemini GenerateContent
            services.AddKeyedTransient<IGeminiGenerateContentService, GeminiGenerateContentService>(
                nameof(GeminiGenerateContentService));
            
            #endregion
            
            #region Image

            services.AddKeyedTransient<IImageService, AzureOpenAIServiceImageService>(
                nameof(AzureOpenAIServiceImageService));

            #endregion

            #region Embedding

            services.AddKeyedTransient<ITextEmbeddingService, SiliconFlowTextEmbeddingService>(
                nameof(SiliconFlowTextEmbeddingService));

            #endregion

            //ai模型特殊性兼容处理
            Configure<SpecialCompatibleOptions>(options =>
            {
                options.Handles.Add(request =>
                {
                    if (request.Model == "gpt-5.1-chat")
                    {
                        request.Temperature = null;
                        request.TopP = null;
                        request.MaxCompletionTokens = request.MaxTokens;
                        request.MaxTokens = null;
                        request.PresencePenalty = null;
                    }
                });
                
                options.Handles.Add(request =>
                {
                    if (request.Model =="gpt-5-mini")
                    {
                        request.Temperature = null;
                        request.TopP = null;
                    }
                });
                options.Handles.Add(request =>
                {
                    if (request.Model == "databricks-claude-sonnet-4")
                    {
                        request.PresencePenalty = null;
                    }
                });
                
                options.Handles.Add(request =>
                {
                    if (request.Model == "o1")
                    {
                        request.Temperature = null;
                    }
                });
                options.Handles.Add(request =>
                {
                    if (request.Model.StartsWith("o3-mini") || request.Model.StartsWith("o4-mini"))
                    {
                        request.MaxCompletionTokens = request.MaxTokens;
                        request.MaxTokens = null;
                        request.Temperature = null;
                    }
                });
                options.Handles.Add(request =>
                {
                    if (request.Stream == true)
                    {
                        request.StreamOptions = new ThorStreamOptions()
                        {
                            IncludeUsage = true
                        };
                    }
                });
                options.Handles.Add(request =>
                {
                    if (request.MaxTokens > 128000)
                    {
                        request.MaxTokens = 128000;
                    }
                });
                options.AnthropicHandles.add(request =>
                {
                    if (request.MaxTokens is null || request.MaxTokens <= 0)
                    {
                        throw new UserFriendlyException("MaxTokens must be greater than or equal to 0");
                    }
                });
            });


            //配置支付宝支付
            var config = configuration.GetSection("Alipay").Get<Config>();
            Factory.SetOptions(config);

            //配置服务号
            Configure<FuwuhaoOptions>(configuration.GetSection("Fuwuhao"));

            services.AddHttpClient()
                .ConfigureHttpClientDefaults(builder =>
                {
                    builder.ConfigureHttpClient(client =>
                    {
                        client.DefaultRequestHeaders.Add("User-Agent", "Apifox/1.0.0 (https://apifox.com)");
                        client.Timeout = TimeSpan.FromMinutes(10);
                    });
                });
        }
    }
}