module IdCardPrinterService
    class EmployerDataFormatter

        def initialize(pl_plan_key, member_keys)
            # @employer = employer
            @member_keys = 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

            @batch_id
        end

        def call
            # blank_card = IdCard::PrintData.new()
            # @employer_cards = []

            base_card = initialize_employer_base_card()
            create_plan_base_cards(base_card)
            
            card_futures = create_member_cards_async()

            card_futures

        end

        private

        def initialize_employer_base_card

            employer_attributes = {
                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
            }
            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
            )

            selected_attributes = employer_attributes.merge(rx_attributes).merge(provider_attributes)
            # blank_card.assign_attributes(selected_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

            mmember_card_futures = @member_keys.map do |member_key|
                Concurrent::Future.execute do
                    ActiveRecord::Base.connection_pool.with_connection do
                        ProcessIdCardDataJob.perform_now(member_key, @card_setup.has_divisions)
                    end
                end
            end
            mmember_card_futures
            # batch_process = BatchProcess.create!(total_jobs: @member_keys.count)
            # @member_keys.each do |member_key|
            #     ProcessIdCardDataJob.perform_later(member_key, @card_setup.has_divisions, batch_process.id)
            # end

            # @batch_id = batch_process.id
            # @members.each do |me|
            #     effect_date = determine_eff_date(me)
            #     if effect_date
            #         member_card = @base_card.dup
            #         member_attributes = {
            #             full_name: me.id_card_display_name,
            #             full_name_last_name_first: me.name,
            #             primary_mb_member_key: me.pb_entity_key,
            #             family_id: me.family_id,
            #             plan_id: me.id_card_plan_id,
            #             medical_eff_date: effect_date.strftime("%m/%d/%Y")
            #         }

            #         if @card_setup.has_divisions
            #             member_attributes.merge!({employer_name: me.division})
            #         end

            #         dependent_attributes = get_dependent_fields(me)
            #         if dependent_attributes.present?
            #             selected_attributes = member_attributes.merge(dependent_attributes)
            #         else
            #             selected_attributes = member_attributes
            #         end

            #         member_card.assign_attributes(selected_attributes)
            #         @employer_cards.push(member_card)
            #     end
            # end
        end

        # def set_network_fields
        #     selected_attributes = @employer.card_provider.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
        #     )
        #     @employer_cards.all do |card|
        #         card.assign_attributes(selected_attributes)
        #     end
        # end

        # def set_rx_fields
        #     # fairos_information = Vhcs::HlrxCrosRef.where(pl_plan_key: 52).first
        #     selected_attributes = @employer.card_rx.attributes.with_indifferent_access.slice(
        #         :customer_service, 
        #         :web_url
        #     )
        #     @employer_cards.all do |card|
        #         card.assign_attributes(selected_attributes)
        #     end
        # end

        def determine_eff_date(member)

            participation = Vhcs::PbProductParticipation.joins('INNER JOIN "PBCoveredEntities" ON "PBProductParticipation"."PBProductParticipationKey" = "PBCoveredEntities"."PBProductParticipationKey"').where('"PBCoveredEntities"."PBEntityKey" = ?', member.pb_entity_key).last
            in_effect = participation.in_effect
            out_of_effect = participation.out_of_effect

            if in_effect <= (Date.today + 90.days) && (out_of_effect - 1.day) > Date.today && out_of_effect > in_effect
                in_effect
            else
                false
            end

        end

        def get_dependent_fields(member)
            dependent_attributes = {}
            dependents = @group_dependents.where(family_id: member.family_id).where.not(pb_entity_key: member.pb_entity_key)
            dependents.each do |dep|
                dependent_name = dep.first_name + ' ' + dep.last_name
                dependent_attributes["dependent_#{dep.sequence_number - 1}".to_sym] = dependent_name
            end
            dependent_attributes
        end
    end
end