class Employer < ApplicationRecord
  include EmployerAutomation
  has_many :members, dependent: :destroy
  accepts_nested_attributes_for :members, allow_destroy: true, reject_if: :all_blank
  has_one :id_card_setup, class_name: 'IdCard::Setup', dependent: :destroy
  has_many :plans, class_name: 'IdCard::Plan', through: :id_card_setup

  scope :active, -> { where(active: true) }
  scope :inactive, -> { where(active: false) }

  before_save :create_slug, if: :will_save_change_to_name?
  before_save :active_initialized_check, if: :will_save_change_to_active?


  def create_slug
    self.slug = Employer.employer_trim_name(name).parameterize
  end

  def active_initialized_check
    if active
      self.initialized = true
    end
    if active == false
      id_card_setup&.update(active: false)
    end
  end

  def id_card_enabled?
    self.id_card_setup.present?
  end

  def claims_check_enabled?
    false
  end

  def employer_member_keys
    {
      pl_plan_key: self.pl_plan_key,
      member_keys: self.members.pluck(:pb_entity_key)
    }
  end

  def name_to_logo_filename(extension)
    Employer.employer_trim_name(self.name).titleize.gsub(/[^a-zA-Z]/, '').concat('Logo').concat(extension.downcase)
  end

  def self.employer_trim_name(employer_name)
    # employer_name = name.present? ? name : self.name
    regex_source  = Regexp.union(["health ", "plan", "the", "inc", "llc", "group"]).source
    case_insensitive_regex = Regexp.new(regex_source, "i")
    employer_name.gsub(case_insensitive_regex, "").gsub(/[^[:alpha:][:space:]]/, "").squish
  end

  def self.permitted_params(params)
    params.require(:employer).permit(
      :name,
      :slug,
      :group_number,
      :pl_plan_key,
      :effective_date
    )
  end

  def save_to_prod

    VhcsRecord.transaction do
      Vhcs::HlPlanCode.create!(
        group_number: self.group_number,
        medical_number: self.group_number,
        dental_number: '',
        plan_key: self.pl_plan_key,
        effect_date: DateTime.strptime(self.effective_date, "%m/%d/%y")
      )

      rx_info = self.id_card_setup.rx_section
      Vhcs::HlrxCrosRef.create!(
        group_no: self.group_number,
        rx_group_id: self.group_number,
        help_desk: rx_info.help_desk,
        customer_service: rx_info.customer_service,
        web_url: rx_info.web_url,
        pl_plan_key: self.pl_plan_key
      )

      self.id_card_setup.plans.each_with_index do |plan, i|
        plan.plan_benefits.each do |bene|
          Vhcs::HlEgglestonCardBenefit.create!(
            plan_id: plan.pb_product_key,
            benefit_desc: bene.benefit_desc,
            benefit: bene.benefit,
            sequence: bene.sequence,
            plan_key: self.pl_plan_key
          )
        end
      end
    end

  end
  
  def build_plan_with_default_benefits(attributes = {})
    plan = plans.new(attributes)
    benefits = IdCardBenefitsTemplate.find_by(title: "BLANK").id_card_benefits.sort_by(&:sequence)
    benefits.each do |ben|
      plan.plan_benefits.new(benefit_desc: ben.benefit_desc, sequence: ben.sequence)
    end
    plan
  end

  private


end
