# Build choice → follow_up_question_id map and set of all follow-up question ids
all_questions = @survey.wk_survey_questions.includes(:wk_survey_choices)
choice_link_map = {}
followup_question_ids = Set.new
all_questions.each do |q|
  q.wk_survey_choices.each do |c|
    if c.follow_up_question_id.present?
      choice_link_map[c.id] = c.follow_up_question_id
      followup_question_ids.add(c.follow_up_question_id)
    end
  end
end
questions_map = all_questions.index_by(&:id)

api.wk_survey do
  api.id @survey.id
  api.name @survey.name
  api.status @survey.status
  api.is_review @survey.is_review
  api.use_points @survey.use_points

  # Build questions grouped by their survey group
  questions = @survey.wk_survey_questions
                  .includes(:wk_survey_que_group, :wk_survey_choices)
                  .order("wk_survey_questions.lft, wk_survey_questions.id")
  questions_by_group = questions.group_by { |q| q.wk_survey_que_group }

  api.array :groups do
    group_counter = 0
    ungrouped_q_counter = 0
    
    questions_by_group.each do |group, group_questions|
      api.groups do
        if group.present? && group.name.present?
          group_counter += 1
          api.id group.id
          api.name group.name
        else
          api.id 0
          api.name ""
        end
        
        api.array :questions do
          question_counter = 0
          
          # Recursive lambda to emit questions and their follow-ups
          emit_survey_question = nil
          emit_survey_question = lambda do |q, depth, parent_index|
            api.questions do
              api.id q.id
              api.name q.name
              api.header q.header
              api.footer q.footer
              api.question_type q.question_type
              api.is_mandatory q.is_mandatory
              api.is_reviewer_only q.is_reviewer_only
              api.depth depth
              api.index parent_index
              
              api.array :choices do
                q.wk_survey_choices.each do |c|
                  api.choices do
                    api.id c.id
                    api.name c.name
                    api.points c.points
                    api.follow_up_question_id c.follow_up_question_id
                  end
                end
              end
            end
            
            # Recurse through choice follow-ups
            q.wk_survey_choices.each_with_index do |c, c_idx|
              fup_id = choice_link_map[c.id]
              if fup_id.present? && questions_map[fup_id]
                child_index = "#{parent_index.chomp('.').presence || ''}.#{c_idx + 1}"
                emit_survey_question.call(questions_map[fup_id], depth + 1, child_index)
              end
            end
          end

          # Emit top-level questions for this group
          top_level_questions = group_questions.select { |q| !followup_question_ids.include?(q.id) }
          top_level_questions.each do |q|
            if group.present? && group.name.present?
              question_counter += 1
              parent_index = "#{group_counter}.#{question_counter}."
            else
              ungrouped_q_counter += 1
              parent_index = "#{ungrouped_q_counter}."
            end
            emit_survey_question.call(q, 0, parent_index)
          end
        end
      end
    end
  end

  api.response do
    api.survey_id @survey.id
    api.user_id User.current.id
    if @response.present?
      api.id @response.id
      api.user_id @response.user_id
      api.survey_for_type @response.survey_for_type
      api.survey_for_id @response.survey_for_id
      api.total_points @response.total_points

      if @response.wk_survey_answers.present?
        api.array :answers do
          @response.wk_survey_answers.each do |a|
            api.answers do
              api.id a.id
              api.survey_question_id a.survey_question_id
              api.survey_choice_id a.survey_choice_id
              api.choice_text a.choice_text
              api.points a.points
            end
          end
        end
      end

      if @response.wk_survey_reviews.present?
        api.array :reviews do
          @response.wk_survey_reviews.each do |r|
            api.reviews do
              api.id r.id
              api.user_id r.user_id
              api.survey_question_id r.survey_question_id
              api.comment_text r.comment_text
            end
          end
        end
      end

    elsif @survey.survey_for_type == "User"
      api.survey_for_type "User"
      api.survey_for_id User.current.id
    elsif params[:surveyForID].present?
      api.survey_for_type params[:surveyForType]
      api.survey_for_id params[:surveyForID]
    end
  end

  api.headers do
    if @response.present? && @response.current_status.present?
      s = @response.current_status
      api.response_status do
        api.id s.id
        api.status_for_type s.status_for_type
        api.status_for_id s.status_for_id
        api.status s.status
        api.status_date s.status_date.strftime("%Y-%m-%d")
        api.responsed_by @response.user.name
      end
    end
    api.isReview @isReview
    api.isReviewed @isReviewed
  end
end