module EmployerAutomation
  extend ActiveSupport::Concern

  included do

    scope :uninitialized, -> {
      where(initialized: false)
    }

    scope :initialized, -> {
      where(initialized: true)
    }

    scope :not_automation_ready, -> {
      where(group_number: [nil, ''])
    }

    scope :automation_ready, -> {
      where.not(group_number: [nil, ''])
    }

    scope :in_automation_initilization, -> {
      joins(:id_card_setup)
        .where(initialized: false)
        .or(
          where(id_card_setup: {initialized: false})
        )
    }

    # scope :missing_keychain_values, -> {
    #   where(company_pb_entity_key: [nil, ''])
    #     .or(where(plan_id: [nil, '']))
    #     .or(where(group_number: [nil, '']))
    #     .or(where(pl_plan_key: [nil, '']))
    # }

    scope :missing_keychain_initialization, -> {
      uninitialized.automation_ready
    }

    scope :uninitialized_id_card_setup, -> {
      joins(:id_card_setup)
        .where(id_card_setup: {initialized: false})
    }

    scope :missing_plans, -> {
        where.missing(:plans)
          .or(
            where(
              id: joins(:plans)
                .where(plans: { pb_product_key: [nil, ''] })
                .select(:id)
            )
          )
    }

    scope :has_plans, -> {
      where.associated(:plans)
      .where.not(
        id: joins(:plans)
          .where(plans: { pb_product_key: [nil, ''] })
          .select(:id)
      ).distinct
    }

    scope :missing_plans_initialization, -> {
      uninitialized_id_card_setup.missing_plans
    }

    scope :missing_members_initialization, -> {
      uninitialized_id_card_setup.has_plans
    }

    scope :with_active_id_card_setup, -> {
      active.left_outer_joins(:id_card_setup)
        .where(id_card_setup: {active: true})
    }

    # scope :with_keychain_values, -> {
    #   where.not(
    #     pl_plan_key: [nil, ''], 
    #     group_number: [nil, ''],
    #     company_pb_entity_key: [nil, ''], 
    #     plan_id: [nil, '']
    #   )
    # }

    scope :deactivated, -> {
      inactive.initialized
    }
  #   Employer.joins(:id_card_setup)
  # .left_outer_joins(:plans)
  # .where(plans: { pb_product_key: [nil, ''] })

  # .group('users.id')                 # Group by user ID
  # .having('COUNT(posts.id) = 0')

  # Employer.left_outer_joins(:plans)
  #     .where(plans: { id: nil })
  #     .or(Employer.where(plans: { pb_product_key: [nil, ''] }))

  # Employer.where.missing(:plans)
  # .or(Employer.joins(:plans).where(plans: { pb_product_key: [nil, ''] }))

  # Employer.joins(id_card_setup: :plans).where.not("plans.id_card_setup_id = id_card_setup.id").or
  # (where(plans: { pb_product_key: [nil, ''] }))

  #   scope :with_survey_and_no_questions, -> {
  #   joins(:survey) # join has_one
  #     .left_joins(survey: :questions) # join has_many through has_one
  #     .where(questions: { id: nil })  # filter where has_many is empty
  # }

    # scope :missing_initial_members, -> {
    #   new_groups.with_plans
    # }
  end

#   class_methods do
#     # Methods in this block become class methods of the including class.
#     def count_all_visible
#       visible.count
#     end
#   end

  # Any other methods defined here become instance methods automatically.
  # def up_to_date?
  #   self.pl_plan_key.present? &&
  #   self.company_pb_entity_key.present? &&
  #   self.plan_id.present? &&
  #   self.group_number.present? &&
  #   self.effective_date.present?
  # end

  def sync_members_with_vhcs
    AutomationService::EmployerMembersUpdate.new(pl_plan_key).call
  end

  def sync_plans_with_vhcs
    AutomationService::EmployerPlansUpdate.new(pl_plan_key).call
  end

  def automation_identifier
    attributes.with_indifferent_access.slice(
      pl_plan_key.present? ? :pl_plan_key : :group_number
    )
  end

  def sync_with_vhcs
    employer_identifier = automation_identifier
    AutomationService::EmployerUpdate.new(employer_identifier).call
  end
end