<commit_before>﻿using System;
using System.Reflection;
using System.Collections.Concurrent;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
using Cap.Consistency.Consumer;
using Cap.Consistency.Consumer.Kafka;

namespace Cap.Consistency
{
    /// <summary>
    /// Helper functions for configuring consistency services.
    /// </summary>
    public class ConsistencyBuilder
    {
        /// <summary>
        /// Creates a new instance of <see cref="ConsistencyBuilder"/>.
        /// </summary>
        /// <param name="message">The <see cref="Type"/> to use for the message.</param>
        /// <param name="service">The <see cref="IServiceCollection"/> to attach to.</param>
        public ConsistencyBuilder(Type message, IServiceCollection service) {
            MessageType = message;
            Services = service;

            AddConsumerServices();
        }

        /// <summary>
        /// Gets the <see cref="IServiceCollection"/> services are attached to.
        /// </summary>
        /// <value>
        /// The <see cref="IServiceCollection"/> services are attached to.
        /// </value>
        public IServiceCollection Services { get; private set; }

        /// <summary>
        /// Gets the <see cref="Type"/> used for messages.
        /// </summary>
        /// <value>
        /// The <see cref="Type"/> used for messages.
        /// </value>
        public Type MessageType { get; private set; }

        public virtual ConsistencyBuilder AddConsumerServices() {

            var IConsumerListenerServices = new Dictionary<Type, Type>();
            foreach (var rejectedServices in Services) {
                if (rejectedServices.ImplementationType != null && typeof(IConsumerService).IsAssignableFrom(rejectedServices.ImplementationType))
                    IConsumerListenerServices.Add(typeof(IConsumerService), rejectedServices.ImplementationType);
            }

            foreach (var service in IConsumerListenerServices) {
                Services.AddSingleton(service.Key, service.Value);
            }

            var types = Assembly.GetEntryAssembly().ExportedTypes;
            foreach (var type in types) {
                if (typeof(IConsumerService).IsAssignableFrom(type)) {
                    Services.AddSingleton(typeof(IConsumerService), type);
                }
            }
            return this;
        }

        public virtual ConsistencyBuilder AddKafkaServices() {


            Services.AddSingleton<IConsumerHandler, KafkaConsumerHandler>();
            return this;
        }


        /// <summary><commit_msg>refactor<commit_after>﻿using System;
using System.Reflection;
using System.Collections.Concurrent;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
using Cap.Consistency.Consumer;
using Cap.Consistency.Routing;
using Cap.Consistency.Infrastructure;
using Cap.Consistency.Internal;
using Cap.Consistency.Abstractions;

namespace Cap.Consistency
{
    /// <summary>
    /// Helper functions for configuring consistency services.
    /// </summary>
    public class ConsistencyBuilder
    {
        /// <summary>
        /// Creates a new instance of <see cref="ConsistencyBuilder"/>.
        /// </summary>
        /// <param name="message">The <see cref="Type"/> to use for the message.</param>
        /// <param name="service">The <see cref="IServiceCollection"/> to attach to.</param>
        public ConsistencyBuilder(Type message, IServiceCollection service) {
            MessageType = message;
            Services = service;

            AddConsumerServices();

            AddKafkaServices();
        }

        /// <summary>
        /// Gets the <see cref="IServiceCollection"/> services are attached to.
        /// </summary>
        /// <value>
        /// The <see cref="IServiceCollection"/> services are attached to.
        /// </value>
        public IServiceCollection Services { get; private set; }

        /// <summary>
        /// Gets the <see cref="Type"/> used for messages.
        /// </summary>
        /// <value>
        /// The <see cref="Type"/> used for messages.
        /// </value>
        public Type MessageType { get; private set; }

        public virtual ConsistencyBuilder AddConsumerServices() {

            var IConsumerListenerServices = new Dictionary<Type, Type>();
            foreach (var rejectedServices in Services) {
                if (rejectedServices.ImplementationType != null && typeof(IConsumerService).IsAssignableFrom(rejectedServices.ImplementationType))
                    IConsumerListenerServices.Add(typeof(IConsumerService), rejectedServices.ImplementationType);
            }

            foreach (var service in IConsumerListenerServices) {
                Services.AddSingleton(service.Key, service.Value);
            }

            var types = Assembly.GetEntryAssembly().ExportedTypes;
            foreach (var type in types) {
                if (typeof(IConsumerService).IsAssignableFrom(type)) {
                    Services.AddSingleton(typeof(IConsumerService), type);
                }
            }

            Services.AddSingleton<IConsumerExcutorSelector, ConsumerExcutorSelector>();        
            Services.AddSingleton<IConsumerInvokerFactory, ConsumerInvokerFactory>();
            Services.AddSingleton<MethodMatcherCache>();

            return this;
        }

        public virtual ConsistencyBuilder AddKafkaServices() {

            return AddScoped(typeof(ITopicRoute), typeof(ConsumerHandler<>).MakeGenericType(MessageType));
        }


        /// <summary>
{'fraction_non_alphanumeric': 0.11067892503536068, 'fraction_numerical': 0.0, 'mean_word_length': 2.26239907727797, 'pattern_counts': {'":': 0, '<': 58, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6607403', 'n_tokens_mistral': 1379, 'n_tokens_neox': 1358, 'n_words': 334}