class EmployerSetupController < ApplicationController
  def new
    @employer_data = session[:employer_data] || {}
    # @id_card_templates = IdCardTemplate.where.not(title: "BLANK")
    # @id_card_template_benefits = IdCardTemplate.find_by(title: "BLANK").id_card_template_benefits.sort_by(&:sequence)
  end

  def create_employer
    @employer_data = {employer: {}}
    @employer_data[:employer].merge!(params.require(:employer).permit(
                      :name, 
                      :group_number,
                      :pl_plan_key,
                      :effective_date
                    ))
    @employer_data[:employer].merge!(params.permit(:number_of_plans))

    session[:employer_data] = @employer_data
    puts session[:employer_data]
    redirect_to action: :plans
  end

  def plans
    @employer_data = session[:employer_data] || {}
    @id_card_templates = IdCardBenefitsTemplate.where.not(title: "BLANK")
    @id_card_template_benefits = IdCardBenefitsTemplate.find_by(title: "BLANK").id_card_benefits.sort_by(&:sequence)
  end

  def create_plans
    @employer_data = session[:employer_data] || {}
    @employer_data.merge!(params.require(:plans).permit(plans_params))
    session[:employer_data] = @employer_data
    redirect_to action: :networks
  end

  def networks
    @employer_data = session[:employer_data] || {}

  end

  def create_provider_networks
    @employer_data = session[:employer_data] || {}
    @employer_data.merge!(params.require(:plans).permit(plans_params))
    session[:employer_data] = @employer_data
    redirect_to action: :networks
  end


  def process_bad_name
    @final_data = session[:employer_data]

    # Vhcs::HlPlanCode.create(
    #   group_number: @final_data['employer']['name'],
    #   medical_number: @final_data['employer']['group_number'],
    #   dental_number: ' ',
    #   plan_key: @final_data['employer']['pl_plan_key'],
    #   effective_date: @final_data['employer']['effective_date']
    # )

    # default = Vhcs::HLRXCrosRef.find_by(pl_plan_key: 52)

    # Vhcs::HLRXCrosRef.create(
    #   group_no: @final_data['employer']['group_number'],
    #   rx_group_id: @final_data['employer']['group_number'],
    #   help_desk: default.help_desk,
    #   customer_service: default.customer_service,
    #   web_url: default.web_url,
    #   pl_plan_key: @final_data['employer']['pl_plan_key']
    # )

    # plans_data = @final_data['plans']
    # benefit_descs = plans_data.delete('benefit_descs')

    # plans_data.each do |key, value|
    #   plan_id = value.delete('plan_id')
    #   value.each do |key2, value2|
    #     Vhcs::HLEgglestonCardBenefit.create(
    #       plan_id: plan_id,
    #       benefit_desc: benefit_descs[key2],
    #       benefit: value2,
    #       sequence: key2,
    #       plan_key: @final_data['employer']['pl_plan_key']
    #     )
    #   end

    # end

    # Create or update your model with @final_data
    # Clear the session data after successful save
    session[:employer_data] = nil
    # Redirect to a success page
  end

  private

  def plans_params
    plans_keys = params[:plans]&.keys || []

    plans_keys.each_with_object({}) do |key, hash|
      if key == 'benefit_descs' || key.match?(/^plan_\d$/)
        hash[key.to_sym] = permited_plan_param_list
      end
    end
  end

  def permited_plan_param_list
    (1..14).map { |i| i.to_s.to_sym }.push(:plan_id)
  end
end
