module IdCardPrinterService
    class CardDataFormatter

        def initialize(employers_member_keys)
            @employers_member_keys = Array.wrap(employers_member_keys)
        end

        def call
            print_card_futures = []
            @employers_member_keys.each do |emk|
                pl_plan_key = emk[:pl_plan_key]
                @member_keys = emk[:member_keys]
                @employer = Employer.includes(id_card_setup: [:provider_section, :rx_section]).find_by(pl_plan_key: pl_plan_key)
                @card_setup = @employer.id_card_setup

                base_card = initialize_employer_base_card()
                create_plan_base_cards(base_card)
                
                employer_card_futures = create_member_cards_async()

                print_card_futures << employer_card_futures
            end 


            print_card_futures.flatten.map(&:value)

        end

        private

        def initialize_employer_base_card

            employer_attributes = {
                employer_id: @employer.id,
                pl_plan_key: @employer.pl_plan_key,
                group_number: @employer.group_number,
                rx_group: @card_setup.rx_group_number,
                network_provider: @card_setup.network_provider,
                network_logo_filename: @card_setup.network_logo.filename,
                employer_logo_filename: @card_setup.employer_logo.filename
            }
            unless @card_setup.has_divisions
                employer_attributes.merge!({employer_name: @card_setup.print_name})
            end

            rx_attributes = @card_setup.rx_section.attributes.with_indifferent_access.slice(
                :customer_service, 
                :web_url
            )

            provider_attributes = @card_setup.provider_section.attributes.with_indifferent_access.slice(
                :provider_line_1, :provider_line_2, :provider_line_3, :provider_line_4, :provider_line_5, :provider_line_6,
                :provider_line_7, :provider_line_8, :provider_line_9, :provider_line_10, :provider_line_11, :provider_line_12,
                :claim_to_1, :claim_to_2, :claim_to_3, :claim_to_4, :claim_to_5, :claim_to_6,
                :claim_to_7, :claim_to_8, :claim_to_9, :claim_to_10, :claim_to_11, :claim_to_12,
                :precert_1, :precert_2, :precert_3, :precert_4
            )

            selected_attributes = employer_attributes.merge(rx_attributes).merge(provider_attributes)
            IdCard::PrintData.new(selected_attributes)
        end

        def create_plan_base_cards(common_fields_card)
            needed_plans_ids = @employer.members.where(pb_entity_key: @member_keys).distinct.pluck(:id_card_plan_id)
            needed_plans = @card_setup.plans.where(id: needed_plans_ids)
            needed_plans.each do |plan|
                selected_attributes = { plan_id: plan.id }
                plan.plan_benefits.each do |bene|
                    selected_attributes["benefit_desc_#{bene.sequence}".to_sym] = bene.benefit_desc
                    selected_attributes["benefit_#{bene.sequence}".to_sym] = bene.benefit
                end

                plan_base_card = common_fields_card.dup
                plan_base_card.assign_attributes(selected_attributes)
                plan_base_card.save
            end
        end

        def create_member_cards_async
            field_exceptions_map = @card_setup.field_exceptions_card_attributes_by_member_id(@member_keys)
            mmember_card_futures = @member_keys.map do |member_key|
                member_card_exceptions_attrs = {}
                member_card_exception_id = field_exceptions_map[member_key]
                if member_card_exception_id
                    member_card_exceptions_attrs = field_exceptions_map[member_card_exception_id]
                end
                Concurrent::Future.execute do
                    ActiveRecord::Base.connection_pool.with_connection do
                        ProcessMemberCardDataJob.perform_now(member_key, member_card_exceptions_attrs, @card_setup.has_divisions, @card_setup.has_dental)
                    end
                end
            end
            
            mmember_card_futures
        end

    end
end