class UpdateMemberJob < ApplicationJob
  queue_as :default

  def perform(pb_entity_key, employer_id, has_divisions = false, has_dental = false, vw_mb_member = {})

    unless vw_mb_member.present?
        vw_mb_member = Vhcs::VwmbMember.find_by(enrollee_type_value_id: 1, pb_entity_key: pb_entity_key).attributes.with_indifferent_access.slice(:mb_member_key, :pb_entity_key, :pl_plan_key, :family_id, :full_name_last_name_first, :social_security_number)
    end

    pb_products = 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" = ? 
        AND "PBProductParticipation"."InEffect" <= ? 
        AND "PBProductParticipation"."OutOfEffect" > ?', 
        vw_mb_member[:pb_entity_key], 2.month.from_now , 1.day.ago
    )

    if pb_products.present? && vw_mb_member[:social_security_number].present?
        member = Member.find_or_create_by(pb_entity_key: vw_mb_member[:pb_entity_key], employer_id: employer_id)
        member.name = vw_mb_member[:full_name_last_name_first].titleize
        member.mb_member_key = vw_mb_member[:mb_member_key]
        member.family_id = vw_mb_member[:family_id]
        member.pl_plan_key = vw_mb_member[:pl_plan_key]

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

        if has_divisions
            division = Vhcs::PbEntity.joins('
                    INNER JOIN "PBAffiliation" ON "PBAffiliation"."ParentPBEntityKey" = "PBEntity"."PBEntityKey" 
                    INNER JOIN "PBProductParticipation" ON "PBProductParticipation"."PBAffiliationKey" = "PBAffiliation"."PBAffiliationKey" 
                    INNER JOIN "PBCoveredEntities" ON "PBProductParticipation"."PBProductParticipationKey" = "PBCoveredEntities"."PBProductParticipationKey"
                ').find_by('
                    "PBCoveredEntities"."PBEntityKey" = ? 
                    AND "PBProductParticipation"."OutOfEffect" > ?', 
                    member.pb_entity_key, 1.day.ago
                ).last_name
            member.division = division
        end

        if has_dental
            medical_pb_product_key = pb_products.where("ShortDescription LIKE ?", "%Medical%")&.first&.pb_product_key
            dental_pb_product_key = pb_products.where("ShortDescription LIKE ?", "%Dental%")&.first&.pb_product_key
            # dental_pb_product_key = pb_products.where(short_description: "Dental")&.first&.pb_product_key
            if dental_pb_product_key
                member.dental_plan_key = dental_pb_product_key

                coverage_class = Vhcs::GenLookupTables.joins('
                    INNER JOIN "PBProductParticipation" ON "PBProductParticipation"."CoverageTypeCode" = "GEN_LookupTables"."RecordID" 
                    INNER JOIN "PBCoveredEntities" ON "PBProductParticipation"."PBProductParticipationKey" = "PBCoveredEntities"."PBProductParticipationKey"
                    INNER JOIN "PBProductAvailability" ON "PBProductAvailability"."PBProductAvailabilityKey" = "PBProductParticipation"."PBProductAvailabilityKey"
                    INNER JOIN "PBProduct" ON "PBProduct"."PBProductKey" = "PBProductAvailability"."PBProductKey"
                ').find_by('
                    "PBCoveredEntities"."PBEntityKey" = ? 
                    AND "PBProductParticipation"."InEffect" <= ? 
                    AND "PBProductParticipation"."OutOfEffect" > ? 
                    AND "PBProduct"."PBProductKey" = ?', 
                    member.pb_entity_key, 1.month.from_now, 1.day.ago, dental_pb_product_key
                ).short_desc
                member.coverage_class = coverage_class
            end
        else
            medical_pb_product_key = pb_products.first.pb_product_key
        end
        if medical_pb_product_key && plan = IdCard::Plan.find_by(pb_product_key: medical_pb_product_key)
            member.id_card_plan = plan
        end
        member.dependents = get_dependent_names_by_sequence(member)
        # if employer_members_update
        #     member
        # else

        # end
        member.save!
        puts "---- #{member.name}"
    end
    member.presence

  end

  private

  def get_dependent_names_by_sequence(member)
    dependents = Vhcs::VwmbMember.joins('
        INNER JOIN "PBCoveredEntities" ON "PBCoveredEntities"."PBEntityKey" = "vwMBMember"."PBEntityKey"
        INNER JOIN "PBProductParticipation" ON "PBProductParticipation"."PBProductParticipationKey" = "PBCoveredEntities"."PBProductParticipationKey"
        INNER JOIN "PBProductAvailability" ON "PBProductAvailability"."PBProductAvailabilityKey" = "PBProductParticipation"."PBProductAvailabilityKey"
        INNER JOIN "PBProduct" ON "PBProduct"."PBProductKey" = "PBProductAvailability"."PBProductKey"
    ').where('
        "EnrolleeTypeKey" != 1044 AND "PLPlanKey" = ? AND "PBProductParticipation"."OutOfEffect" >= ? AND "FamilyID" = ?', member.pl_plan_key, Date.today.strftime("%m/%d/%Y"), member.family_id
    )
    if member.pl_plan_key == 3
        dependents = dependents.where('"PBProduct"."PBProductKey" != 1024')
    elsif member.pl_plan_key == 2
        dependents = dependents.where('"PBProduct"."PBProductKey" != 1019')
    end
    dependent_names = dependents.order(:sequence_number).map { |dep| dep.first_name + ' ' + dep.last_name}.uniq
    dependent_names
end


end