<commit_before># frozen_string_literal: true

module Autocomplete
  class Result
    attr_reader :id, :code, :display_name
    def initialize(id:, code:, display_name:)
      @id = id
      @code = code
      @display_name = display_name
    end
  end

  class Dhis2
    def initialize(project_anchor)
      @project_anchor = project_anchor
    end

    def search(name, kind: "data_elements", limit: 20)
      Dhis2Snapshot.connection.select_all(
        ActiveRecord::Base.send(
          :sanitize_sql_array,
          [SEARCH_QUERY, kind, @project_anchor.id, "%#{name}%", limit]
        )
      ).to_hash.map { |e| Result.new(id: e["id"], code: e["code"], display_name: e["display_name"]) }
    end

    def find(id, kind: "data_elements")
      Dhis2Snapshot.connection.select_all(
        ActiveRecord::Base.send(
          :sanitize_sql_array,
          [FIND_QUERY, kind, @project_anchor.id, id]
        )
      ).to_hash.map { |e| Result.new(id: e["id"], code: e["code"], display_name: e["display_name"]) }
    end

    FIND_QUERY = "select * from (
      select
        (element->'table'->>'id')::text as id ,
            (element->'table'->>'display_name')::text as display_name,
           (element->'table'->>'code')::text as code
        from (
             select jsonb_array_elements(content) as element
             from dhis2_snapshots
             where
             id=(
                SELECT max(id) FROM dhis2_snapshots
                WHERE kind= ? AND project_anchor_id = ?)
             )
         as elements
      ) as dataelements
       where id = ?"

    SEARCH_QUERY = "select * from (
      select
        (element->'table'->>'id')::text as id ,
            (element->'table'->>'display_name')::text as display_name,
           (element->'table'->>'code')::text as code
        from (
             select jsonb_array_elements(content) as element
             from dhis2_snapshots
             where
             id=(
                SELECT max(id) FROM dhis2_snapshots
                WHERE kind= ? AND project_anchor_id = ?)
             )
         as elements
      ) as dataelements
       where display_name ilike ?
      limit ?"
  end<commit_msg>Refactor search query to allow for multiple fields<commit_after># frozen_string_literal: true

module Autocomplete
  class Result
    attr_reader :id, :code, :display_name, :category_combo
    def initialize(id:, code:, display_name:, category_combo: nil)
      @id = id
      @code = code
      @display_name = display_name
      @category_combo = category_combo
    end
  end

  class Dhis2
    def initialize(project_anchor)
      @project_anchor = project_anchor
    end

    # Skip default
    def search(name, kind: "data_elements", limit: 20, fields: [:id, :display_name, :code])
      Dhis2Snapshot.from(
        Dhis2Snapshot.select("jsonb_array_elements(content) as element").from(
          Dhis2Snapshot.where(id: Dhis2Snapshot.where(kind: kind).select("max(id)").where(project_anchor_id: @project_anchor))
        )).where("element->'table'->>'display_name' ILIKE ?", "%#{name}%").limit(limit).pluck(
        "element->'table'->>'id'::text",
        "element->'table'->>'display_name'",
        "element->'table'->>'code'",
        "element->'table'->>'category_option_combos'"
      ).map do |id, display_name, code,  _category_option_combos|
        Result.new(id: id, code: code, display_name: display_name, category_combo: nil)
      end
    end

    def find(id, kind: "data_elements")
      Dhis2Snapshot.connection.select_all(
        ActiveRecord::Base.send(
          :sanitize_sql_array,
          [FIND_QUERY, kind, @project_anchor.id, id]
        )
      ).to_hash.map { |e| Result.new(id: e["id"], code: e["code"], display_name: e["display_name"]) }
    end

    FIND_QUERY = "select * from (
      select
        (element->'table'->>'id')::text as id ,
            (element->'table'->>'display_name')::text as display_name,
           (element->'table'->>'code')::text as code
        from (
             select jsonb_array_elements(content) as element
             from dhis2_snapshots
             where
             id=(
                SELECT max(id) FROM dhis2_snapshots
                WHERE kind= ? AND project_anchor_id = ?)
             )
         as elements
      ) as dataelements
       where id = ?"
  end
{'fraction_non_alphanumeric': 0.12722235049642114, 'fraction_numerical': 0.004386977603324867, 'mean_word_length': 2.327188940092166, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 45, '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': '953342', 'n_tokens_mistral': 1359, 'n_tokens_neox': 1334, 'n_words': 289}