class EmployerSetupNetworkExceptionsForm
  include ActiveModel::Model
  include ActiveModel::Attributes

  Network_exception = Struct.new(:network_logo, :exceptions)
  attribute :network_exceptions, :array_of_items, default: -> { [] }
  attribute :pl_plan_key, :string
  
  attr_accessor :employer_setup_process_id

  # validates :network_exceptions, presence: true if number_of_additional_network_logos > 0

  def initialize(employer_setup_process_id, params = {})
    @employer_setup_process_id = employer_setup_process_id
    if params.present?
      form_params = permitted_params(params)
      super(form_params)
    else
      super(params)
    end

  end

  def permitted_params(params)
    params.require(:employer_setup_network_exceptions_form).permit(
      network_exceptions: [
        :network_logo, 
        exceptions: [
          :type, 
          :value
        ]
      ]
    )
  end

  # def self.permitted_params
  #   [
  #     network_exceptions: [
  #       :network_logo, 
  #       exceptions: [
  #         :type, 
  #         :value
  #       ]
  #     ]
  #   ]
  # end

  def pull_from_employer_setup_form(attributes = {})
    if attributes['pl_plan_key']
      self.pl_plan_key = attributes['pl_plan_key']
    end
    if attributes['number_of_additional_network_logos']
      self.number_of_additional_network_logos = attributes['number_of_additional_network_logos']
    end
  end

  def process_for_save(network_exceptions)
    ne_array = []
    network_exceptions.first.each do |ne|
      processed_ne = ne.last
      exceptions_array = []
      processed_ne['exceptions'].each do |e|
        exceptions_array.push(e.last)
      end
      processed_ne['exceptions'] = exceptions_array
      ne_array.push(processed_ne)
    end
    ne_array
  end

  def save
    # Implement logic to save data to models after all steps are complete
    # For example, create a User record with the collected data
    if valid?
      process = EmployerSetupProcess.find(@employer_setup_process_id)
      # employer = BrittonWeb::Employers.find_by(pl_plan_key: pl_plan_key)
      if network_exceptions
        network_exceptions_data = process_for_save(network_exceptions)
      end
      # network_exceptions_data.each do |ne|
      #   ne['exceptions'].each do |ex|
      #     BrittonWeb::NetworkLogos.create!(
      #       employer_id: employer.id,
      #       net_logo: ne['network_logo'].original_filename,
      #       exception_type: ex['type'].downcase,
      #       exception_value: ex['value'],
      #       default: false
      #     )
      #   end
      # end

      true
    else
      false
    end
  end

end


class NetworkException
  include ActiveModel::Model
  include ActiveModel::Attributes

  attribute :network_logo
  attribute :exceptions, array: true, default: -> { [] }
end