class ProcessMemberCardDataJob < ApplicationJob
  queue_as :default

  def perform(member_key, member_card_exceptions_attrs, has_divisions = false, has_dental = false)
    member = Member.find_by(pb_entity_key: member_key)

    effect_date = determine_eff_date(member)
    if effect_date
      member_card = @base_card.dup
        member_attributes = {
            full_name: member.id_card_display_name,
            full_name_last_name_first: member.name,
            primary_mb_member_key: member.pb_entity_key,
            family_id: member.family_id,
            plan_id: member.id_card_plan_id,
            medical_eff_date: effect_date.strftime("%m/%d/%Y")
        }

        if member.dependents.present?
            dependent_attributes = format_dependent_attributes(member)
            member_attributes.merge!(dependent_attributes)
        end

        if has_divisions
            member_attributes.merge!({employer_name: member.division})
        end

        if member_card_exceptions_attrs.present?
            # exceptions_attributes = {}
            # field_exceptions = IdCard::FieldException.where(id: field_exception_ids)
            # member_exception_values = member.id_card_field_exception_values
            # field_exceptions.each do |fe|
            #     if fe.exception_values.include?(member_exception_values[fe.exception_type.to_sym])
            #         fe.field_exception_items.each do |fei|
            #             if fei.field_value.present?
            #                 exception_eff_date = Date.strptime(fei.field_value, "%m/%d/%Y")
            #                 member_eff_date = Date.strptime(member_attributes[:medical_eff_date], "%m/%d/%Y")
            #                 if exception_eff_date > member_eff_date
            #                     exceptions_attributes[fei.field_name.to_sym] = fei.field_value
            #                 end
            #             elsif fei.provider_section_id.present?
            #                 provider_attributes = IdCard::ProviderSection.find(fei.provider_section_id).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
            #                 )
            #                 exceptions_attributes.merge!(provider_attributes)
            #             elsif fei.network_logo_id.present?
            #                 exceptions_attributes.merge!(network_logo_filename: fei.network_logo.filename)
            #             end
            #         end
            #     end
            # end
            member_attributes.merge!(member_card_exceptions_attrs)
        end

        if has_dental
            member_attributes.merge!({dental_coverage: member.coverage_class.upcase})
            if member.dental_plan_key && member.id_card_plan_id.blank?
                member_attributes.merge!({group_number: "", medical_eff_date: ""})
                # dental_plan = IdCard::Plan.find_by(pb_product_key: 1025)
                if IdCard::Plan.find_by(pb_product_key: member.dental_plan_key).blank?
                    member_attributes.merge!({employer_name: 'COBRA'})
                end
            end
        end

        if member.id_card_plan.present? && member.id_card_plan.title.upcase.include?('COBRA')
            member_attributes.merge!({employer_name: 'COBRA'})
        end

        if member.id_card_plan_id.present?
            member_card = IdCard::PrintData.find_by(pl_plan_key: member.pl_plan_key, plan_id: member.id_card_plan_id, primary_mb_member_key: nil).dup
        else
            member_card = IdCard::PrintData.where(pl_plan_key: member.pl_plan_key, primary_mb_member_key: nil).first.dup
        end

        member_card.assign_attributes(member_attributes)
        member_card.save
        # if dependent_attributes.present?
        #     dependent_card = member_card.dup
        #     dependent_card.full_name_last_name_first = dependent_card.full_name_last_name_first.concat(" dependent")
        #     dependent_card.save
        # end
    end

    true

    # BatchProcess.increment_counter(:completed_jobs, batch_process_id)
    
  end

  private

  def determine_eff_date(member)
    participation = Vhcs::PbProductParticipation.joins('INNER JOIN "PBCoveredEntities" ON "PBProductParticipation"."PBProductParticipationKey" = "PBCoveredEntities"."PBProductParticipationKey"').where('"PBCoveredEntities"."PBEntityKey" = ?', member.pb_entity_key).order(out_of_effect: :desc).first
    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 format_dependent_attributes(member)
    dependent_attributes = {}
    member.dependents.each_with_index do |dep, index|
        dependent_attributes["dependent_#{index + 1}".to_sym] = dep
    end
    dependent_attributes
  end

end