using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using NPP.SmartSchedue.Api.Contracts.Services.Personnel;
using NPP.SmartSchedue.Api.Services.Personnel;
using NPP.SmartSchedue.Api.Contracts.Services.Integration;
using NPP.SmartSchedue.Api.Services.Integration;
using NPP.SmartSchedue.Api.Contracts.Services.Equipment;
using NPP.SmartSchedue.Api.Services.Equipment;
using NPP.SmartSchedue.Api.Contracts.Domain.Equipment;
using NPP.SmartSchedue.Api.Repositories.Equipment;
using NPP.SmartSchedue.Api.Repositories.Personnel;
using NPP.SmartSchedue.Api.Contracts.Services.Notification;
using NPP.SmartSchedue.Api.Services.Notification;
using NPP.SmartSchedue.Api.Contracts.Domain.Notification;
using NPP.SmartSchedue.Api.Repositories.Notification;
using NPP.SmartSchedue.Api.Contracts;
using NPP.SmartSchedue.Api.Services.Integration.Algorithms;
using System;
using System.Linq;
using System.Net.Http;
using Polly;

namespace NPP.SmartSchedue.Api
{
    /// <summary>
    /// 服务集合扩展方法
    /// </summary>
    public static class ServiceCollectionExtensions
    {
        /// <summary>
        /// 注册人员约束相关服务
        /// </summary>
        public static IServiceCollection AddPersonnelConstraintServices(this IServiceCollection services)
        {
            return services;
        }

        /// <summary>
        /// 注册设备相关Repository服务
        /// </summary>
        public static IServiceCollection AddEquipmentRepositories(this IServiceCollection services)
        {
            // 注册设备相关Repository
            services.AddScoped<IEquipmentRepository, EquipmentRepository>();
            services.AddScoped<IEquipmentMaintenanceRepository, EquipmentMaintenanceRepository>();
            services.AddScoped<IEquipmentCalibrationRepository, EquipmentCalibrationRepository>();
            
            // 注册人员资质Repository
            services.AddScoped<IQualificationRepository, QualificationRepository>();
            services.AddScoped<IPersonnelQualificationRepository, PersonnelQualificationRepository>();

            return services;
        }

        /// <summary>
        /// 注册算法引擎服务
        /// </summary>
        public static IServiceCollection AddAlgorithmEngines(this IServiceCollection services)
        {
            // 注册三个核心算法引擎
            services.AddScoped<InputValidationEngine>();
            services.AddScoped<ContextBuilderEngine>();
            services.AddScoped<GeneticAlgorithmEngine>();
            services.AddScoped<GlobalOptimizationEngine>();
            services.AddScoped<ShiftRuleValidationEngine>();
            services.AddScoped<LinearProgrammingEngine>();

            return services;
        }

        /// <summary>
        /// 注册通知相关服务
        /// </summary>
        public static IServiceCollection AddNotificationServices(this IServiceCollection services)
        {
            // 注册通知相关Repository
            services.AddScoped<INotificationSettingRepository, NotificationSettingRepository>();
            services.AddScoped<IPersonnelGroupRepository, PersonnelGroupRepository>();
            services.AddScoped<INotificationHistoryRepository, NotificationHistoryRepository>();
            
            // 注册通知相关Service
            services.AddScoped<INotificationService, NotificationService>();
            services.AddScoped<IEmailNotificationService, EmailNotificationService>();
            services.AddScoped<ISystemMessageService, SystemMessageService>();
            services.AddScoped<INotificationTemplateService, NotificationTemplateService>();

            return services;
        }

        /// <summary>
        /// 注册工作订单整合操作相关服务
        /// </summary>
        public static IServiceCollection AddWorkOrderIntegrationOperationServices(this IServiceCollection services)
        {
            // 注册状态验证器
            services.AddScoped<WorkOrderStatusValidator>();

            // 注册统一操作服务接口和实现
            services.AddScoped<IWorkOrderIntegrationOperationService, WorkOrderIntegrationOperationService>();

            return services;
        }
    }
}