<commit_before>class SmartAnswersController < ApplicationController
  include Slimmer::Headers

  before_action :find_smart_answer, except: %w[index]
  before_action :redirect_response_to_canonical_path, only: %w[show]
  before_action :setup_content_item, except: %w[index]

  attr_accessor :content_item

  rescue_from SmartAnswer::FlowRegistry::NotFound, with: :error_404
  rescue_from SmartAnswer::InvalidNode, with: :error_404

  content_security_policy only: :visualise do |p|
    # The script used to render the visualise tool requires eval execution
    # unfortunately
    p.script_src(*p.script_src, :unsafe_eval)
  end

  def index
    @flows = flow_registry.flows.sort_by(&:name)
    @title = "Smart Answers Index"
    @content_item = {}
  end

  def show
    @title = @presenter.title

    render page_type, formats: [:html]

    set_expiry
  end

  def visualise
    respond_to do |format|
      format.html do
        @graph_presenter = GraphPresenter.new(@smart_answer)
        @title = @presenter.title
        @graph_data = @graph_presenter.to_hash
        render layout: "application"
      end

      format.gv do
        render plain: GraphvizPresenter.new(@smart_answer).to_gv,
               content_type: "text/vnd.graphviz"
      end
    end
  end

private

  def find_smart_answer
    @name = params[:id].to_sym
    @smart_answer = flow_registry.find(@name.to_s)
    state = SmartAnswer::StateResolver.new(@smart_answer).state_from_params(request.params)
    @presenter = FlowPresenter.new(@smart_answer, state)
  end

  def flow_registry
    @flow_registry = SmartAnswer::FlowRegistry.instance
  end

  def page_type
    return :landing unless params[:started]
    return :result if @presenter.finished?

    :question
  end
  helper_method :page_type

  def redirect_response_to_canonical_path
    if params[:next] && !@presenter.state.error
      set_expiry<commit_msg>Remove landing page logic from SmartAnswerController<commit_after>class SmartAnswersController < ApplicationController
  include Slimmer::Headers

  before_action :find_smart_answer, except: %w[index]
  before_action :redirect_response_to_canonical_path, only: %w[show]
  before_action :setup_content_item, except: %w[index]

  attr_accessor :content_item

  rescue_from SmartAnswer::FlowRegistry::NotFound, with: :error_404
  rescue_from SmartAnswer::InvalidNode, with: :error_404

  content_security_policy only: :visualise do |p|
    # The script used to render the visualise tool requires eval execution
    # unfortunately
    p.script_src(*p.script_src, :unsafe_eval)
  end

  def index
    @flows = flow_registry.flows.sort_by(&:name)
    @title = "Smart Answers Index"
    @content_item = {}
  end

  def show
    @title = @presenter.title

    render page_type, formats: [:html]

    set_expiry
  end

  def visualise
    respond_to do |format|
      format.html do
        @graph_presenter = GraphPresenter.new(@smart_answer)
        @title = @presenter.title
        @graph_data = @graph_presenter.to_hash
        render layout: "application"
      end

      format.gv do
        render plain: GraphvizPresenter.new(@smart_answer).to_gv,
               content_type: "text/vnd.graphviz"
      end
    end
  end

private

  def find_smart_answer
    @name = params[:id].to_sym
    @smart_answer = flow_registry.find(@name.to_s)
    state = SmartAnswer::StateResolver.new(@smart_answer).state_from_params(request.params)
    @presenter = FlowPresenter.new(@smart_answer, state)
  end

  def flow_registry
    @flow_registry = SmartAnswer::FlowRegistry.instance
  end

  def page_type
    return :result if @presenter.finished?

    :question
  end
  helper_method :page_type

  def redirect_response_to_canonical_path
    if params[:next] && !@presenter.state.error
      set_expiry
{'fraction_non_alphanumeric': 0.08498284507785696, 'fraction_numerical': 0.003167062549485352, 'mean_word_length': 3.7493734335839597, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5384858', 'n_tokens_mistral': 1243, 'n_tokens_neox': 1178, 'n_words': 264}