class EmployerSetupController < ApplicationController
  def new
    # session.delete(:employer_setup_data)
    @form = EmployerSetupForm.new(session[:employer_setup_data])
    if @form.current_step == "plans"
      @id_card_templates = IdCardBenefitsTemplate.where.not(title: "BLANK")
      @id_card_template_benefits = IdCardBenefitsTemplate.find_by(title: "BLANK").id_card_benefits.sort_by(&:sequence)
    end
    puts session[:employer_setup_data]
    render "employer_setup/#{@form.current_step}" # Renders the view for the current step
  end

  def create
    filtered_params = employer_setup_params.except(:validation_context)
    @form = EmployerSetupForm.new(filtered_params)

    if @form.current_step == "general" && @form.valid?(:general_info)
      # @form.current_step = "plans"
      @form.current_step = "networks" # TESTING, Change Back
      session[:employer_setup_data] = @form.attributes.slice("current_step", "name", "employer_logo", "group_number", "pl_plan_key", "effect_date", "number_of_plans", "network", "number_of_additional_network_logos")
      redirect_to new_employer_setup_path # Redirect to the next step
    elsif @form.current_step == "plans" && @form.valid?(:plan_info)
      if @form.number_of_additional_network_logos == 0
        next_step = "summary"
      else
        next_step = "networks"
      end
      @form.current_step = next_step
      session[:employer_setup_data].merge!(@form.attributes.slice("current_step", "plans", "benefit_descs"))
      redirect_to new_employer_setup_path
      # @form = UserOnboardingForm.new(session[:employer_setup_data]) # Re-initialize with all data
    elsif @form.current_step == "networks" && @form.valid?(:network_info)
      @form.current_step = "summary"
      session[:employer_setup_data].merge!(@form.attributes.slice("current_step", "network_exceptions"))
      redirect_to new_employer_setup_path
    elsif @form.current_step == "summary"
      puts @form.attributes
      if @form.save
        session.delete(:employer_setup_data) # Clear session data after successful save
        redirect_to root_path, notice: "Employer setup successfully!"
      else
        render "employer_setup/summary" # Render step two again with errors
      end
    else
      render "employer_setup/#{@form.current_step}" # Render the current step again with errors
    end
  end

  private

  def employer_setup_params
    params.require(:employer_setup_form).permit(
      :current_step,
      :name,
      :employer_logo,
      :group_number,
      :pl_plan_key,
      :effect_date,
      :number_of_plans,
      :network,
      :number_of_additional_network_logos,
      network_exceptions: [:network_logo, exceptions: [:type, :value]],
      plans: permited_plans_keys,
      benefit_descs: benefit_sequence_keys
    )
  end

  def benefit_sequence_keys
    (1..14).map { |i| i.to_s.to_sym }
  end

  def permited_plans_keys
    benefit_sequence_keys.push(:plan_id)
  end

  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
