namespace :member do
  desc "TODO"
  # rake member:import_from_vhcs[123456,1]
  task :import_from_vhcs, [:mb_member_key, :employer_id] => :environment do |t, args|
    mb_member_key = args[:mb_member_key]
    employer_id = args[:employer_id]

    Member.find_or_create_by!(mb_member_key: mb_member_key) do |me|
            me.name = vwm.full_name_last_name_first.titleize
            me.pb_entity_key = vwm.pb_entity_key
            me.family_id = vwm.family_id
            me.pl_plan_key = vwm.pl_plan_key
            me.employer_id = employer_id

            card_display_name = Vhcs::PbEntity.find_by(pb_entity_key: me.pb_entity_key).full_name
            me.id_card_display_name = card_display_name

            plan_pb_product_key = Vhcs::PbProduct.joins('INNER JOIN "PBProductAvailability" ON "PBProductAvailability"."PBProductKey" = "PBProduct"."PBProductKey" INNER JOIN "PBProductParticipation" ON "PBProductParticipation"."PBProductAvailabilityKey" = "PBProductAvailability"."PBProductAvailabilityKey" INNER JOIN "PBCoveredEntities" ON "PBProductParticipation"."PBProductParticipationKey" = "PBCoveredEntities"."PBProductParticipationKey"').where('"PBCoveredEntities"."PBEntityKey" = ?', me.pb_entity_key).first.pb_product_key
            if plan = Plan.find_by(pb_product_key: plan_pb_product_key)
                me.plan = plan
            end
        end

  end

end
