class EmployerSetupGeneralInformationForm
  include ActiveModel::Model
  include ActiveModel::Attributes

  attribute :name, :string
  attribute :employer_logo
  attribute :group_number, :string
  attribute :dental, :boolean, default: false
  attribute :pl_plan_key, :string
  attribute :effective_date, :string
  # attribute :number_of_plans, :integer
  attribute :network, :string
  # attribute :number_of_additional_network_logos, :integer

  attr_accessor :employer_setup_process_id

  validates :name, presence: true
  validates :employer_logo, presence: true
  validates :group_number, presence: true
  validates :effective_date, presence: true
  # validates :number_of_plans, presence: true
  validates :network, presence: true
  # validates :number_of_additional_network_logos, presence: true if network = "cigna+"

  # def initialize(attributes = {})
  # if attributes
  #   permitted_attributes = form_strong_params(attributes)
  #   super(permitted_attributes)
  # end

  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)
      load_from_process
    end
  end

  def load_from_process
    process = EmployerSetupProcess.find(@employer_setup_process_id)

    self.name = process.employer_name
    # self.employer_logo = process.
    self.group_number = process.group_number
    self.dental = false
    self.pl_plan_key = process.pl_plan_key
    self.effective_date = process.effective_date
    self.network = process.network_provider
  end

  def permitted_params(params)
    params.require(:employer_setup_general_information_form).permit(
      :name,
      :employer_logo,
      :group_number,
      :dental,
      :pl_plan_key,
      :effective_date,
      :number_of_plans,
      :network,
      :number_of_additional_network_logos
    )
  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)
      process.update(
        employer_name: name,
        group_number: group_number,
        pl_plan_key: pl_plan_key,
        effective_date: effective_date,
        network_provider: network
      )

      # hl_plan_code = Vhcs::HlPlanCode.create!(
      #   group_number: group_number,
      #   medical_number: group_number,
      #   dental_number: '',
      #   plan_key: pl_plan_key,
      #   effect_date: effective_date
      # )

      # # Replace fairos_info with template like for benefits
      # fairos_info = Vhcs::HlrxCrosRef.where(pl_plan_key: 52).first
      # hlrx_cros_ref = Vhcs::HlrxCrosRef.create!(
      #   group_no: group_number,
      #   rx_group_id: group_number,
      #   help_desk: fairos_info.help_desk,
      #   customer_service: fairos_info.customer_service,
      #   web_url: fairos_info.web_url,
      #   pl_plan_key: pl_plan_key
      # )

      # web_employer = BrittonWeb::Employers.create!(
      #   name: name,
      #   pl_plan_key: pl_plan_key,
      #   dental_plan: dental,
      #   single_card_template: 'FairosRxIDCard',
      #   logo: employer_logo.original_filename
      # )

      # default_network_logo =
      #   case
      #   when network.include?("cig")
      #     "CignaLogo.png"
      #   when network.include?("med")
      #     "Logo_MC_PMS.png"
      #   else
      #     "CignaLogo.png"
      #   end

      # BrittonWeb::NetworkLogos.create!(
      #   employer_id: web_employer.id,
      #   net_logo: default_network_logo,
      #   default: true
      # )

      true
    else
      false
    end
  end

end