module ReportsService
    class DuplicateOldCards

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

        def call
            @pl_plan_keys.each do |pl_plan_key|
                Vhcs::HlidCardEggData.destroy_all
                clear_out_queue()
                CallStoredProc.new('HLQueueAllIDCardsByPlan', { PLPlanKey: pl_plan_key }).call
                puts "---------------------------------------------------------------------------------"
                puts "---------------------------------------------------------------------------------"
                puts "use ID Cards app to pull queued for group #{pl_plan_key}, then press enter."
                puts "---------------------------------------------------------------------------------"
                puts "---------------------------------------------------------------------------------"
                continue = gets

                old_cards = Vhcs::HlidCardEggData.where(pl_plan_key: pl_plan_key)
                old_cards.each do |card|
                    duplicate_card(card)
                end

            end

        end

        private

        def duplicate_card(card)
            unless Report::OldCardDuplicate.exists?(primary_mb_member_key: card.primary_mb_member_key)
                dup_attrs = card.attributes.with_indifferent_access.slice(
                    :employer_name, :full_name, :group_number, :medical_eff_date, :dental_coverage, :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,:family_id, :rx_group, :customer_service, :web_url, :benefit_desc_1, 
                    :benefit_1, :benefit_desc_2, :benefit_2, :benefit_desc_3, :benefit_3, :benefit_desc_4, :benefit_4, :benefit_desc_5, :benefit_5, 
                    :benefit_desc_6, :benefit_6, :benefit_desc_7, :benefit_7, :benefit_desc_8, :benefit_8, :benefit_desc_9, :benefit_9, :benefit_desc_10, 
                    :benefit_10, :benefit_desc_11, :benefit_11, :benefit_desc_12, :benefit_12, :benefit_desc_13, :benefit_13, :benefit_desc_14, :benefit_14, 
                    :dependent_1, :dependent_2, :dependent_3, :dependent_4, :dependent_5, :dependent_6, :dependent_7, :dependent_8, :pl_plan_key, :primary_mb_member_key
                )
                Report::OldCardDuplicate.create(dup_attrs)
            end
        end

        def clear_out_queue
            VhcsRecord.connection.execute("
                UPDATE CLDocument
                SET DocStatus = 'Printed', PrintDate = GetDate()
                where DocStatus <> 'Printed'
            ")
        end

    end
end
