This file is a merged representation of the entire codebase, combining all repository files into a single document.
Generated by Repomix on: 2024-11-13T02:46:57.745Z

================================================================
File Summary
================================================================

Purpose:
--------
This file contains a packed representation of the entire repository's contents.
It is designed to be easily consumable by AI systems for analysis, code review,
or other automated processes.

File Format:
------------
The content is organized as follows:
1. This summary section
2. Repository information
3. Repository structure
4. Multiple file entries, each consisting of:
  a. A separator line (================)
  b. The file path (File: path/to/file)
  c. Another separator line
  d. The full contents of the file
  e. A blank line

Usage Guidelines:
-----------------
- This file should be treated as read-only. Any changes should be made to the
  original repository files, not this packed version.
- When processing this file, use the file path to distinguish
  between different files in the repository.
- Be aware that this file may contain sensitive information. Handle it with
  the same level of security as you would the original repository.

Notes:
------
- Some files may have been excluded based on .gitignore rules and Repomix's
  configuration.
- Binary files are not included in this packed representation. Please refer to
  the Repository Structure section for a complete list of file paths, including
  binary files.
- Code comments have been removed.

Additional Info:
----------------

For more information about Repomix, visit: https://github.com/yamadashy/repomix

================================================================
Repository Structure
================================================================
assets/js/app.js
assets/js/autocomplete.js
lib/moldb_web/live/components/pagination_component.ex
lib/moldb_web/live/components/search_form.ex
lib/moldb_web/live/components/search_results.ex
lib/moldb_web/live/home_live.ex
lib/moldb_web/live/search_live.ex
lib/moldb/compounds.ex
lib/moldb/compounds/compound_property.ex
lib/moldb/compounds/compound_synonym.ex
lib/moldb/compounds/compound.ex
lib/moldb/compounds/substance.ex
lib/moldb/search.ex

================================================================
Repository Files
================================================================

================
File: assets/js/app.js
================
import "phoenix_html"

import {Socket} from "phoenix"
import {LiveSocket} from "phoenix_live_view"
import topbar from "../vendor/topbar"
import Autocomplete from "./autocomplete"


let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
let liveSocket = new LiveSocket("/live", Socket, {
  longPollFallbackMs: 2500,
  params: {_csrf_token: csrfToken},
  hooks: {Autocomplete}
})


topbar.config({barColors: {0: "#29d"}, shadowColor: "rgba(0, 0, 0, .3)"})
window.addEventListener("phx:page-loading-start", _info => topbar.show(300))
window.addEventListener("phx:page-loading-stop", _info => topbar.hide())


liveSocket.connect()





window.liveSocket = liveSocket

================
File: assets/js/autocomplete.js
================
const Autocomplete = {
  mounted() {
    this.setupEventListeners();
  },

  setupEventListeners() {
    this.handleInput();
    this.handleFocus();
    this.handleClickOutside();
    this.handleSuggestionUpdates();
  },

  handleInput() {
    this.el.addEventListener("input", () => {
      this.pushEventTo("#search_form", "suggest", {query: this.el.value});
    });
  },

  handleFocus() {
    this.el.addEventListener("focus", () => {
      if (this.el.value.length > 0) {
        this.pushEventTo("#search_form", "suggest", {query: this.el.value});
      }
    });
  },

  handleClickOutside() {
    document.addEventListener("click", (e) => {
      if (!this.el.contains(e.target)) {
        this.hideSuggestions();
      }
    });
  },

  handleSuggestionUpdates() {
    this.handleEvent("update_suggestions", ({suggestions}) => {
      this.showSuggestions(suggestions);
    });
  },

  showSuggestions(suggestions) {
    const suggestionsEl = document.getElementById("suggestions");
    suggestionsEl.innerHTML = "";

    if (suggestions.length > 0) {
      suggestionsEl.classList.remove("hidden");
      this.renderSuggestions(suggestions, suggestionsEl);
    } else {
      this.hideSuggestions();
    }
  },

  renderSuggestions(suggestions, container) {
    suggestions.forEach((suggestion) => {
      const li = this.createSuggestionElement(suggestion);
      container.appendChild(li);
    });
  },

  createSuggestionElement(suggestion) {
    const li = document.createElement("li");
    li.textContent = suggestion;
    li.className = "px-4 py-2 cursor-pointer hover:bg-gray-100";
    li.addEventListener("click", () => this.handleSuggestionClick(suggestion));
    return li;
  },

  handleSuggestionClick(suggestion) {
    this.el.value = suggestion;
    this.hideSuggestions();
    this.pushEventTo("#search_form", "search", { query: suggestion });
  },

  hideSuggestions() {
    const suggestionsEl = document.getElementById("suggestions");
    suggestionsEl.classList.add("hidden");
  }
};

export default Autocomplete;

================
File: lib/moldb_web/live/components/pagination_component.ex
================
defmodule MoldbWeb.Components.Pagination do
  use MoldbWeb, :live_component

  @impl true
  def render(assigns) do
    ~H"""
    <div class="mt-4 text-center">
      <%= if @has_more do %>
        <button
          phx-click="load_more"
          class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600 transition-colors"
          disabled={@loading}
        >
          <%= if @loading, do: "Loading...", else: "Load More" %>
        </button>
      <% end %>
    </div>
    """
  end
end

================
File: lib/moldb_web/live/components/search_form.ex
================
defmodule MoldbWeb.Components.SearchForm do
  use MoldbWeb, :live_component

  @impl true
  def render(assigns) do
    ~H"""
    <form phx-submit="search" phx-change="suggest" phx-target={@myself} class="relative">
      <div class="relative">
        <.input
          type="text"
          name="query"
          value={@query}
          placeholder="Search compounds or substances..."
          class="w-full p-4 pr-12 rounded-lg"
          phx-debounce="300"
          phx-hook="Autocomplete"
          autocomplete="off"
        />
        <button type="submit" class="absolute right-2 top-1/2 -translate-y-1/2 p-2">
          <.icon name="hero-magnifying-glass" class="h-5 w-5" />
        </button>
      </div>
      <div id="suggestions" class="absolute z-50 w-full mt-1 bg-white shadow-lg rounded-lg hidden" />
    </form>
    """
  end

  @impl true
  def handle_event("search", %{"query" => query}, socket) when byte_size(query) > 0 do
    {:noreply, push_navigate(socket, to: ~p"/search?query=#{URI.encode(query)}&page=1")}
  end

  @impl true
  def handle_event("suggest", %{"query" => query}, socket) when byte_size(query) >= 2 do
    case Moldb.Search.suggest(query, 5) do
      {:ok, suggestions} ->
        {:noreply, push_event(socket, "update_suggestions", %{suggestions: suggestions})}
      {:error, _} ->
        {:noreply, socket}
    end
  end

  @impl true
  def handle_event("suggest", _, socket), do: {:noreply, socket}
end

================
File: lib/moldb_web/live/components/search_results.ex
================
defmodule MoldbWeb.Components.SearchResults do
  use MoldbWeb, :live_component

  import MoldbWeb.CoreComponents

  @impl true
  def render(assigns) do
    ~H"""
    <div id={@id} class="space-y-4 mt-8">
      <%= if @results_count == 0 do %>
        <.empty_state />
      <% else %>
        <div id="results" phx-update="stream" class="grid gap-4 md:grid-cols-2">
          <.result :for={{dom_id, result} <- @results} id={dom_id} {result} />
        </div>
      <% end %>
    </div>
    """
  end

  attr :id, :string, required: true
  attr :type, :string, required: true
  attr :name, :string, required: true
  attr :description, :string, required: true
  def result(assigns) do
    ~H"""
    <div
      id={@id}
      class="bg-white p-4 rounded-lg shadow hover:shadow-md transition-shadow"
      phx-click={JS.navigate(result_path(@type, @id))}
    >
      <div class="flex items-start justify-between">
        <h3 class="font-semibold text-lg"><%= @name %></h3>
        <.badge color={type_color(@type)}><%= @type %></.badge>
      </div>
      <p class="mt-2 text-sm text-gray-600 line-clamp-2"><%= @description %></p>
    </div>
    """
  end

  def empty_state(assigns) do
    ~H"""
    <div class="text-center py-12">
      <.icon name="hero-magnifying-glass" class="mx-auto h-12 w-12 text-gray-400" />
      <h3 class="mt-2 text-sm font-semibold text-gray-900">No results found</h3>
      <p class="mt-1 text-sm text-gray-500">Try adjusting your search terms</p>
    </div>
    """
  end

  defp type_color("compound"), do: :zinc
  defp type_color("substance"), do: :blue
  defp type_color(_), do: :gray

  defp result_path(type, id) do
    case type do
      "compound" -> ~p"/compound/#{id}"
      "substance" -> ~p"/substance/#{id}"
      _ -> "#"
    end
  end
end

================
File: lib/moldb_web/live/home_live.ex
================
defmodule MoldbWeb.HomeLive do
  use MoldbWeb, :live_view
  
  require Logger

  alias MoldbWeb.Components.SearchForm

  @impl true
  def mount(_params, _session, socket) do
    {:ok, assign(socket, query: "")}
  end

  @impl true
  def render(assigns) do
    ~H"""
    <div class="max-w-2xl mx-auto p-6">
      <h1 class="text-3xl font-bold mb-6 text-center text-gray-800">Welcome to Molecular Database</h1>
      <.live_component module={SearchForm} id="home_search_form" query={@query} />
    </div>
    """
  end

  @impl true
  def handle_event("search", %{"query" => query}, socket) do
    {:noreply, push_navigate(socket, to: ~p"/search?query=#{query}&page=1")}
  end

  @impl true
  def handle_event("suggest", %{"query" => query}, socket) when byte_size(query) >= 2 do
    Logger.debug("Generating suggestions for query: #{query}")
    case Moldb.Search.suggest(query, 5) do
      {:ok, suggestions} ->
        Logger.debug("Generated suggestions: #{inspect(suggestions)}")
        {:noreply, push_event(socket, "update_suggestions", %{suggestions: suggestions})}

      {:error, _reason} ->
        Logger.error("Failed to generate suggestions")
        {:noreply, push_event(socket, "update_suggestions", %{suggestions: []})}
    end
  end

  @impl true
  def handle_event("suggest", _params, socket) do
    {:noreply, push_event(socket, "update_suggestions", %{suggestions: []})}
  end

  @impl true
  def handle_info({:search, query}, socket) do
    # Handle the search query here, e.g., navigate to the search page
    {:noreply, push_navigate(socket, to: ~p"/search?query=#{URI.encode(query)}&page=1")}
  end
end

================
File: lib/moldb_web/live/search_live.ex
================
defmodule MoldbWeb.SearchLive do
  use MoldbWeb, :live_view
  require Logger
  alias Moldb.Search
  alias MoldbWeb.Components.SearchForm
  alias MoldbWeb.Components.SearchResults
  alias MoldbWeb.Components.Pagination

  @per_page 20

  @impl true
  def mount(_params, _session, socket) do
    {:ok,
     socket
     |> assign(query: "", page: 1, has_more: false, loading: false, results_count: 0)
     |> stream(:results, [])}
  end

  @impl true
  def handle_params(params, _url, socket) do
    with {:ok, query} <- extract_query(params),
         {:ok, page} <- extract_page(params) do
      Logger.debug("Processing search with query: #{query}, page: #{page}")

      {:noreply,
       socket
       |> assign(query: query, page: page, loading: true)
       |> fetch_results()}
    else
      _ ->
        {:noreply,
         socket
         |> assign(query: "", page: 1)
         |> stream(:results, [], reset: true)}
    end
  end

  @impl true
  def render(assigns) do
    ~H"""
    <div class="max-w-2xl mx-auto p-6">
      <h1 class="text-3xl font-bold mb-6 text-center text-gray-800">Molecular Database Search</h1>
      <.live_component module={SearchForm} id="search_form" query={@query} />
      <.live_component module={SearchResults} id="search_results" results={@streams.results} results_count={@results_count} />
      <.live_component module={Pagination} id="pagination" has_more={@has_more} page={@page} loading={@loading} />
    </div>
    """
  end

  @impl true
  def handle_event("suggest", %{"query" => query}, socket) when is_binary(query) and byte_size(query) >= 2 do
    Logger.debug("Generating suggestions for query: #{query}")
    case Search.suggest(query, 5) do
      {:ok, suggestions} ->
        Logger.debug("Generated suggestions: #{inspect(suggestions)}")
        {:noreply, push_event(socket, "update_suggestions", %{suggestions: suggestions})}

      {:error, _reason} ->
        Logger.error("Failed to generate suggestions")
        {:noreply, push_event(socket, "update_suggestions", %{suggestions: []})}
    end
  end

  @impl true
  def handle_event("suggest", _params, socket) do
    {:noreply, push_event(socket, "update_suggestions", %{suggestions: []})}
  end

  @impl true
  def handle_event("load_more", _, socket) do
    %{query: query, page: page} = socket.assigns
    {:noreply, push_navigate(socket, to: ~p"/search?query=#{URI.encode(query)}&page=#{page + 1}")}
  end

  # Private functions

  defp extract_query(%{"query" => query}) when is_binary(query), do: {:ok, query}
  defp extract_query(_), do: {:error, :invalid_query}

  defp extract_page(%{"page" => page}) when is_binary(page) do
    case Integer.parse(page) do
      {page_num, ""} when page_num > 0 -> {:ok, page_num}
      _ -> {:error, :invalid_page}
    end
  end
  defp extract_page(_), do: {:ok, 1}  # Default to page 1 if not specified

  defp fetch_results(%{assigns: %{query: query, page: page}} = socket) when byte_size(query) > 0 do
    case Search.search(query, page, @per_page, %{}) do
      {:ok, results} ->
        has_more = length(results) == @per_page
        socket
        |> stream(:results, results, reset: page == 1)
        |> assign(has_more: has_more, loading: false, results_count: length(results))

      {:error, :timeout} ->
        socket
        |> put_flash(:error, "Search timed out. Please try again.")
        |> assign(loading: false)

      {:error, _reason} ->
        socket
        |> put_flash(:error, "An error occurred while searching. Please try again.")
        |> assign(loading: false)
    end
  end

  defp fetch_results(socket) do
    socket
    |> stream(:results, [], reset: true)
    |> assign(has_more: false, loading: false, results_count: 0)
  end
end

================
File: lib/moldb/compounds.ex
================
defmodule Moldb.Compounds do
  @moduledoc """
  Documentation for Moldb.Compounds.
  """
  import Ecto.Query, warn: false
  alias Moldb.Repo
  alias Moldb.Compounds.{Compound, Substance, CompoundProperty}

  def list_compounds(opts \\ []) do
    Compound
    |> maybe_preload(opts[:preload])
    |> Repo.all()
  end

  def get_compound!(id, opts \\ []) do
    Compound
    |> maybe_preload(opts[:preload])
    |> Repo.get!(id)
  end

  def get_compound_with_assocs!(id) do
    Repo.get!(Compound, id)
    |> Repo.preload([:substances, :properties, :synonyms])
  end

  def create_compound(attrs \\ %{}) do
    %Compound{}
    |> Compound.changeset(attrs)
    |> Repo.insert()
  end

  def update_compound(%Compound{} = compound, attrs) do
    compound
    |> Compound.changeset(attrs)
    |> Repo.update()
  end

  def delete_compound(%Compound{} = compound), do: Repo.delete(compound)

  def change_compound(%Compound{} = compound, attrs \\ %{}), do: Compound.changeset(compound, attrs)

  def list_substances(opts \\ []) do
    Substance
    |> maybe_preload(opts[:preload])
    |> Repo.all()
  end

  def get_substance!(id, opts \\ []) do
    Substance
    |> maybe_preload(opts[:preload])
    |> Repo.get!(id)
  end

  def create_substance(attrs \\ %{}) do
    %Substance{}
    |> Substance.changeset(attrs)
    |> Repo.insert()
  end

  def update_substance(%Substance{} = substance, attrs) do
    substance
    |> Substance.changeset(attrs)
    |> Repo.update()
  end

  def delete_substance(%Substance{} = substance), do: Repo.delete(substance)

  def change_substance(%Substance{} = substance, attrs \\ %{}), do: Substance.changeset(substance, attrs)

  def compound_name(%Compound{} = compound), do: compound.name || "Unknown"
  def name(%Substance{} = substance), do: substance.name || "Unknown"

  def list_categories do
    CompoundProperty
    |> Repo.all()
    |> Enum.map(& &1.property_category)
    |> Enum.uniq()
  end

  defp maybe_preload(query, nil), do: query
  defp maybe_preload(query, preloads), do: from(q in query, preload: ^preloads)
end

================
File: lib/moldb/compounds/compound_property.ex
================
defmodule Moldb.Compounds.CompoundProperty do
  use Ecto.Schema
  import Ecto.Changeset

  @primary_key {:id, UUIDv7, autogenerate: true}
  @foreign_key_type :binary_id

  schema "compound_properties" do
    field :property_name, :string
    field :property_value, :decimal
    field :property_unit, :string
    field :property_category, :string

    belongs_to :compound, Moldb.Compounds.Compound

    timestamps()
  end

  @property_categories ["physical", "chemical", "thermodynamic", "mechanical"]

  def changeset(property, attrs) do
    property
    |> cast(attrs, [:property_name, :property_value, :property_unit, :property_category, :compound_id])
    |> validate_required([:property_name, :property_value, :property_category])
    |> validate_inclusion(:property_category, @property_categories)
    |> assoc_constraint(:compound)
  end
end

================
File: lib/moldb/compounds/compound_synonym.ex
================
defmodule Moldb.Compounds.CompoundSynonym do
  @moduledoc """
  Defines the CompoundSynonym schema and changeset functions.
  """
  use Ecto.Schema
  import Ecto.Changeset

  @primary_key {:id, UUIDv7, autogenerate: true}
  @foreign_key_type :binary_id

  schema "compound_synonyms" do
    field :synonym, :string

    belongs_to :compound, Moldb.Compounds.Compound

    timestamps()
  end

  @doc false
  def changeset(synonym, attrs) do
    synonym
    |> cast(attrs, [:synonym, :compound_id])
    |> validate_required([:synonym])
    |> assoc_constraint(:compound)
  end
end

================
File: lib/moldb/compounds/compound.ex
================
defmodule Moldb.Compounds.Compound do
  use Ecto.Schema
  import Ecto.Changeset

  @primary_key {:id, UUIDv7, autogenerate: true}
  schema "compounds" do
    field :name, :string
    field :description, :string
    field :molecular_formula, :string
    field :molecular_weight, :decimal
    field :canonical_smiles, :string
    field :inchi, :string
    field :inchi_key, :string

    has_many :substances, Moldb.Compounds.Substance
    has_many :properties, Moldb.Compounds.CompoundProperty
    has_many :synonyms, Moldb.Compounds.CompoundSynonym

    timestamps(type: :utc_datetime)
  end

  def changeset(compound, attrs) do
    compound
    |> cast(attrs, [:name, :molecular_formula, :molecular_weight, :canonical_smiles, :inchi, :inchi_key])
    |> validate_required([:name, :molecular_formula, :canonical_smiles, :inchi, :inchi_key])
    |> unique_constraint(:canonical_smiles)
    |> unique_constraint(:inchi)
    |> unique_constraint(:inchi_key)
    |> cast_assoc(:properties)
    |> cast_assoc(:synonyms)
  end
end

================
File: lib/moldb/compounds/substance.ex
================
defmodule Moldb.Compounds.Substance do
  use Ecto.Schema
  import Ecto.Changeset

  @primary_key {:id, UUIDv7, autogenerate: true}
  schema "substances" do
    field :name, :string
    field :description, :string
    field :physical_state, :string
    field :salt_form, :string
    field :stereochemistry, :string

    belongs_to :compound, Moldb.Compounds.Compound, type: UUIDv7

    timestamps(type: :utc_datetime)
  end

  def changeset(substance, attrs) do
    substance
    |> cast(attrs, [:name, :physical_state, :salt_form, :stereochemistry, :compound_id])
    |> validate_required([:name, :compound_id])
    |> foreign_key_constraint(:compound_id)
  end
end

================
File: lib/moldb/search.ex
================
defmodule Moldb.Search do
  @moduledoc """
  Provides concurrent search functionality across compounds and substances with improved
  performance and error handling.
  """

  import Ecto.Query
  alias Moldb.Repo
  alias Moldb.Compounds.{Compound, Substance}

  @search_timeout :timer.seconds(5)
  @suggestion_timeout :timer.seconds(2)
  @minimum_query_length 2
  @max_concurrent_tasks 2

  @type search_result :: %{
    type: String.t(),
    id: Ecto.UUID.t(),
    name: String.t(),
    description: String.t()
  }

  @type search_options :: %{
    page: pos_integer(),
    per_page: pos_integer(),
    filters: map()
  }

  @type search_error ::
    :timeout |
    :invalid_query |
    :search_failed |
    :suggestion_failed

  @doc """
  Performs optimized concurrent search across compounds and substances.

  ## Options
    * `:page` - The page number (default: 1)
    * `:per_page` - Results per page (default: 20)
    * `:filters` - Map of filters to apply

  ## Examples
      iex> Search.search("aspirin", %{page: 1, per_page: 20, filters: %{}})
      {:ok, [%{type: "compound", id: "uuid", name: "Aspirin", description: "..."}]}
  """
  @spec search(String.t(), search_options()) :: {:ok, [search_result()]} | {:error, search_error()}
  def search(query, opts \\ %{}) when is_binary(query) do
    with {:ok, sanitized_query} <- validate_and_sanitize_query(query),
         {:ok, options} <- validate_options(opts) do
      perform_concurrent_search(sanitized_query, options)
    end
  end

  @doc """
  Provides intelligent search suggestions from multiple sources.

  ## Examples
      iex> Search.suggest("asp", 5)
      {:ok, ["Aspirin", "Aspartame", "Asparagine"]}
  """
  @spec suggest(String.t(), pos_integer()) :: {:ok, [String.t()]} | {:error, search_error()}
  def suggest(query, limit) when byte_size(query) >= @minimum_query_length do
    with {:ok, sanitized_query} <- validate_and_sanitize_query(query) do
      perform_concurrent_suggestions(sanitized_query, limit)
    end
  end
  def suggest(_, _), do: {:error, :invalid_query}

  # Private functions

  defp perform_concurrent_search(query, %{page: page, per_page: per_page, filters: filters}) do
    offset = (page - 1) * per_page

    search_tasks = [
      {:compounds, fn -> search_source(Compound, query, offset, per_page, filters) end},
      {:substances, fn -> search_source(Substance, query, offset, per_page, filters) end}
    ]

    Task.async_stream(
      search_tasks,
      fn {_type, search_fn} -> search_fn.() end,
      ordered: false,
      max_concurrency: @max_concurrent_tasks,
      timeout: @search_timeout
    )
    |> collect_results()
  end

  defp perform_concurrent_suggestions(query, limit) do
    [Compound, Substance]
    |> Task.async_stream(
      &suggest_from_source(&1, query, limit),
      ordered: false,
      max_concurrency: @max_concurrent_tasks,
      timeout: @suggestion_timeout
    )
    |> collect_and_process_suggestions(limit)
  end

  defp search_source(schema, query, offset, limit, filters) do
    schema
    |> base_search_query(query)
    |> apply_filters(filters)
    |> paginate(offset, limit)
    |> Repo.all()
    |> Enum.map(&format_result(&1, schema))
  end

  defp suggest_from_source(schema, query, limit) do
    schema
    |> select([s], s.name)
    |> where([s], ilike(s.name, ^"#{query}%"))
    |> order_by([s], desc: fragment("similarity(?, ?)", s.name, ^query))
    |> limit(^limit)
    |> Repo.all()
  end

  defp base_search_query(query, search_term) do
    from q in query,
      where: ilike(q.name, ^"%#{search_term}%"),
      or_where: fragment("? @@ plainto_tsquery(?)", q.description, ^search_term),
      select: %{
        id: q.id,
        name: q.name,
        description: q.description
      }
  end

  defp format_result(result, schema) do
    type = get_type_for_schema(schema)
    Map.put(result, :type, type)
  end

  defp get_type_for_schema(Compound), do: "compound"
  defp get_type_for_schema(Substance), do: "substance"

  defp collect_results(stream) do
    Enum.reduce_while(stream, {:ok, []}, fn
      {:ok, results}, {:ok, acc} -> {:cont, {:ok, acc ++ results}}
      {:exit, :timeout}, _ -> {:halt, {:error, :timeout}}
      _, _ -> {:halt, {:error, :search_failed}}
    end)
  end

  defp collect_and_process_suggestions(stream, limit) do
    case collect_results(stream) do
      {:ok, suggestions} -> {:ok, process_suggestions(suggestions, limit)}
      error -> error
    end
  end

  defp process_suggestions(suggestions, limit) do
    suggestions
    |> List.flatten()
    |> Enum.uniq()
    |> Enum.sort_by(&{-String.length(&1), &1})
    |> Enum.take(limit)
  end

  defp apply_filters(query, filters) do
    Enum.reduce(filters, query, fn
      {"status", status}, query -> where(query, [q], q.status == ^status)
      {"category", category}, query -> where(query, [q], q.category == ^category)
      _, query -> query
    end)
  end

  defp paginate(query, offset, limit) do
    query
    |> offset(^offset)
    |> limit(^limit)
  end

  defp validate_and_sanitize_query(query) when byte_size(query) >= @minimum_query_length do
    {:ok,
     query
     |> String.trim()
     |> String.replace(~r/[%_\\]/, fn x -> "\\#{x}" end)}
  end
  defp validate_and_sanitize_query(_), do: {:error, :invalid_query}

  defp validate_options(opts) do
    {:ok, %{
      page: Map.get(opts, :page, 1),
      per_page: Map.get(opts, :per_page, 20),
      filters: Map.get(opts, :filters, %{})
    }}
  end
end
Review and analyze this elixir code from phoenix framework web application using liveview and postgresql. Refine and refactor it to achieve modular, reusable structure, maximal efficiency, high performance, scalability and concurrency. Follow best practices and most recent documentations, ensure code is fully valid, consistent and properly implemented.