using FluentValidation;
using IWS5.Core.Enums;
using IWS5.Core.EventTrigger;
using IWS5.Core.Services;
using IWS5.Core.Services.IAdmin;
using IWS5.DatabaseAccess.Extensions;
using IWS5.LdapClient.Extensions;
using IWS5.Services.EventTrigger;
using IWS5.Services.IAdminServices;
using IWS5.Services.Validators;
using IWS5.Services.Validators.IAdmin;
using IWS5.Signature;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using IWS5.Services.Validators.IAdmin.Reports;
using IWS5.Services.Validators.IAdmin.Schedule;
using IWS5.Services.Validators.IAdmin.Servers;

namespace IWS5.Services.Extensions;

public static class ServiceCollectionExtensions
{
    public static void RegisterServices(this IServiceCollection services, IConfiguration configuration)
    {
        services.RegisterLdapClient();
        services.RegisterDatabaseAccess(configuration);

        services.AddValidatorsFromAssemblyContaining<DocumentTemplateFieldValidator>();
        services.AddValidatorsFromAssemblyContaining<CreateDocumentTemplateRequestValidator>();
        services.AddValidatorsFromAssemblyContaining<UpdateDocumentTemplateRequestValidator>();
        services.AddValidatorsFromAssemblyContaining<UpdateFieldListOfValuesRequestValidator>();
        services.AddValidatorsFromAssemblyContaining<UpdateFieldValueRequestValidator>();
        services.AddValidatorsFromAssemblyContaining<UpdateDocumentFieldsRequestValidator>();
        services.AddValidatorsFromAssemblyContaining<ListProcessAllowedTemplatesRequestValidator>();
        services.AddValidatorsFromAssemblyContaining<CreateNoteRequestItemValidator>();
        services.AddValidatorsFromAssemblyContaining<AddDocumentRequestValidator>();
        services.AddValidatorsFromAssemblyContaining<CreateFolderFieldValueRequestValidator>();
        services.AddValidatorsFromAssemblyContaining<CreateWorkItemRequestValidator>();
        services.AddValidatorsFromAssemblyContaining<ForwardWorkItemRequestValidator>();
        services.AddValidatorsFromAssemblyContaining<LogInCredentialsValidator>();
        services.AddValidatorsFromAssemblyContaining<RefreshTokenRequestValidator>();
        services.AddValidatorsFromAssemblyContaining<SignWorkItemsRequestValidator>();
        services.AddValidatorsFromAssemblyContaining<UpdateDocumentContentRequestValidator>();
        services.AddValidatorsFromAssemblyContaining<RenameDocumentRequestValidator>();
        services.AddValidatorsFromAssemblyContaining<CreateDocumentRequestValidator>();
        services.AddValidatorsFromAssemblyContaining<CloneWorkItemRequestValidator>();

        services.AddTransient<IRightsService, RightsService>();
        services.AddTransient<IAuthenticationService, AuthenticationService>();
        services.AddTransient<IProcessService, ProcessService>();
        services.AddTransient<IQueueService, QueueService>();
        services.AddTransient<IWorkItemService, WorkItemService>();
        services.AddTransient<IDocumentService, DocumentService>();
        services.AddTransient<INoteService, NoteService>();
        services.AddTransient<IFolderFieldService, FolderFieldService>();
        services.AddTransient<IButtonService, ButtonService>();
        services.AddTransient<ITrackingRecordService, TrackingRecordService>();
        services.AddTransient<IConnectionService, ConnectionService>();
        services.AddTransient<IDocumentTemplateService, DocumentTemplateService>();
        services.AddTransient<IProcessDocumentTemplateRightsService, ProcessDocumentTemplateRightsService>();
        services.AddTransient<ISignatureService, SignatureService>();
        
        services.AddKeyedTransient<ITriggerProcessor, EmailTriggerProcessor>(ATTriggerAction.SendEmail);
        services.AddKeyedTransient<ITriggerProcessor, MoveToEventTriggerProcessor>(ATTriggerAction.SendToEvent);
        services.AddKeyedTransient<ITriggerProcessor, MoveToProcessTriggerProcessor>(ATTriggerAction.SendToProcess);
        services.AddKeyedTransient<ITriggerProcessor, AssignFolderFieldValueFromQueryTriggerProcessor>(ATTriggerAction.RunSqlQueryWithResultInFolderField);

        #region AdminApi

        services.AddHttpContextAccessor();
        services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();

        services.AddValidatorsFromAssemblyContaining<CreateReportRequestValidator>();
        services.AddValidatorsFromAssemblyContaining<UpdateReportRequestValidator>();
        services.AddValidatorsFromAssemblyContaining<CreateScheduleRequestValidator>();
        services.AddValidatorsFromAssemblyContaining<ScheduleTaskRequestValidator>();
        services.AddValidatorsFromAssemblyContaining<ScheduleRouteValidator>();
        services.AddValidatorsFromAssemblyContaining<ScheduleTaskRouteValidator>();
        services.AddValidatorsFromAssemblyContaining<ScheduleStatusUpdateQueryValidator>();
        services.AddValidatorsFromAssemblyContaining<CreateServerRequestValidator>();
        services.AddValidatorsFromAssemblyContaining<UpdateServerRequestValidator>();

        services.AddTransient<IReportsService, ReportsService>();
        services.AddTransient<ISchedulesService, SchedulesService>();
        services.AddTransient<IScheduleTasksService, ScheduleTasksService>();
        services.AddTransient<IServersService, ServersService>();

        #endregion
    }
}
