# lib/moldb/search_supervisor.ex
defmodule Moldb.SearchSupervisor do
  use GenServer

  @max_failures 3
  @reset_interval 30

  # Client API

  def start_link(opts) do
    GenServer.start_link(__MODULE__, opts, name: __MODULE__)
  end

  def execute_query(query_fn) do
    GenServer.call(__MODULE__, {:execute, query_fn})
  end

  # Server Callbacks

  def init(_opts) do
    {:ok, %{failures: 0, last_failure: nil}}
  end

  def handle_call({:execute, query_fn}, _from, state) do
    if circuit_open?(state) do
      {:reply, {:error, :circuit_open}, state}
    else
      try do
        result = query_fn.()
        {:reply, {:ok, result}, %{state | failures: 0}}
      rescue
        _ ->
          new_state = %{state | failures: state.failures + 1, last_failure: System.system_time(:second)}
          {:reply, {:error, :query_failed}, new_state}
      end
    end
  end

  # Helper functions

  defp circuit_open?(%{failures: failures, last_failure: last_failure}) do
    failures >= @max_failures and last_failure && System.system_time(:second) - last_failure < @reset_interval
  end
end

# lib/moldb_web/live/search_live.ex
defmodule MoldbWeb.SearchLive do
  use MoldbWeb, :live_view
  alias Moldb.Search
  alias Moldb.SearchSupervisor
  alias MoldbWeb.SearchComponent

  @per_page 20
  @debounce_ms 300
  @rate_limit_interval_ms 1000
  @rate_limit_max_requests 5

  @impl true
  def mount(_params, _session, socket) do
    {:ok,
     socket
     |> assign(results: [], query: "", page: 1, per_page: @per_page)
     |> assign(search_ref: nil, rate_limit_state: init_rate_limit_state())}
  end

  @impl true
  def handle_params(params, _url, socket) do
    {:noreply, apply_action(socket, socket.assigns.live_action, params)}
  end

  defp apply_action(socket, :index, _params) do
    socket
    |> assign(:page_title, "Search Molecular Database")
  end

  @impl true
  def handle_event("load_more", _, %{assigns: %{query: query, page: page, per_page: per_page}} = socket) do
    case SearchSupervisor.execute_query(fn -> Search.search(query, page + 1, per_page) end) do
      {:ok, new_results} ->
        {:noreply, assign(socket, results: socket.assigns.results ++ new_results, page: page + 1)}
      {:error, _reason} ->
        {:noreply, put_flash(socket, :error, "Unable to load more results. Please try again later.")}
    end
  end

  @impl true
  def handle_event("navigate", %{"id" => id, "type" => type}, socket) do
    path = if type == "compound", do: ~p"/compound/#{id}", else: ~p"/substance/#{id}"
    {:noreply, push_navigate(socket, to: path)}
  end

  @impl true
  def handle_info({:search, query}, socket) do
    case check_rate_limit(socket.assigns.rate_limit_state) do
      {:ok, new_state} ->
        handle_search(query, assign(socket, rate_limit_state: new_state))
      {:error, _} ->
        {:noreply, put_flash(socket, :error, "Too many requests. Please wait before trying again.")}
    end
  end

  defp handle_search(query, socket) do
    case SearchSupervisor.execute_query(fn -> Search.search(query, 1, socket.assigns.per_page) end) do
      {:ok, results} ->
        {:noreply, assign(socket, results: results, query: query, page: 1)}
      {:error, _reason} ->
        {:noreply, put_flash(socket, :error, "Search is temporarily unavailable. Please try again later.")}
    end
  end

  defp init_rate_limit_state do
    %{last_reset: System.monotonic_time(:millisecond), request_count: 0}
  end

  defp check_rate_limit(state) do
    now = System.monotonic_time(:millisecond)
    if now - state.last_reset >= @rate_limit_interval_ms do
      {:ok, %{last_reset: now, request_count: 1}}
    else
      if state.request_count < @rate_limit_max_requests do
        {:ok, %{state | request_count: state.request_count + 1}}
      else
        {:error, :rate_limit_exceeded}
      end
    end
  end
end

# lib/moldb_web/live/search_live.html.heex
<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={SearchComponent} id="search" query={@query} />

  <div id="search-results" class="space-y-4 mt-8">
    <%= if Enum.empty?(@results) do %>
      <p class="text-center text-gray-600">No results found. Try a different search term.</p>
    <% else %>
      <%= for result <- @results do %>
        <div class="result bg-white p-4 rounded-lg shadow hover:shadow-md transition-shadow duration-200 ease-in-out cursor-pointer"
             phx-click="navigate"
             phx-value-id={result.id}
             phx-value-type={result.type}>
          <h3 class="text-lg font-semibold text-gray-800 mb-2"><%= result.name %></h3>
          <p class="text-sm text-gray-600"><%= result.description %></p>
          <span class="inline-block mt-2 px-2 py-1 text-xs font-semibold text-gray-700 bg-gray-200 rounded-full">
            <%= String.capitalize(result.type) %>
          </span>
        </div>
      <% end %>
    <% end %>
  </div>

  <%= if length(@results) >= @page * @per_page do %>
    <div class="mt-4 text-center">
      <button phx-click="load_more" class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600 transition-colors">
        Load More
      </button>
    </div>
  <% end %>
</div>

# lib/moldb_web/live/search_component.ex
defmodule MoldbWeb.SearchComponent do
  use MoldbWeb, :live_component
  alias Moldb.Search
  alias Moldb.SearchSupervisor

  @suggestion_limit 5
  @debounce_ms 300

  @impl true
  def render(assigns) do
    ~H"""
    <div>
      <form phx-submit="search" phx-change="suggest" phx-target={@myself} class="relative">
        <div class="relative">
          <input type="text"
                 name="query"
                 value={@query}
                 placeholder="Search for compounds or substances..."
                 autocomplete="off"
                 phx-debounce={@debounce_ms}
                 class="w-full p-4 pr-12 text-sm border-gray-200 rounded-lg shadow-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
          />
          <button type="submit" class="absolute inset-y-0 right-0 px-4 text-gray-600 hover:text-blue-600">
            <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-5 h-5">
              <path stroke-linecap="round" stroke-linejoin="round" d="M21 21l-5.197-5.197m0 0A7.5 7.5 0 105.196 5.196a7.5 7.5 0 0010.607 10.607z" />
            </svg>
          </button>
        </div>

        <%= if @suggestions != [] do %>
          <div class="absolute z-10 w-full mt-2 bg-white border border-gray-200 rounded-lg shadow-lg">
            <%= for suggestion <- @suggestions do %>
              <div class="px-4 py-2 cursor-pointer hover:bg-gray-100" phx-click="select_suggestion" phx-value-query={suggestion} phx-target={@myself}>
                <%= suggestion %>
              </div>
            <% end %>
          </div>
        <% end %>
      </form>
    </div>
    """
  end

  @impl true
  def update(assigns, socket) do
    {:ok, assign(socket, query: assigns[:query] || "", suggestions: [], debounce_ms: @debounce_ms)}
  end

  @impl true
  def handle_event("suggest", %{"query" => query}, socket) when byte_size(query) >= 2 do
    case SearchSupervisor.execute_query(fn -> Search.suggest(query, @suggestion_limit) end) do
      {:ok, suggestions} ->
        {:noreply, assign(socket, suggestions: suggestions, query: query)}
      {:error, _reason} ->
        {:noreply, assign(socket, suggestions: [], query: query)}
    end
  end

  @impl true
  def handle_event("suggest", _, socket), do: {:noreply, assign(socket, suggestions: [])}

  @impl true
  def handle_event("search", %{"query" => query}, socket) do
    send(self(), {:search, query})
    {:noreply, assign(socket, query: query, suggestions: [])}
  end

  @impl true
  def handle_event("select_suggestion", %{"query" => query}, socket) do
    send(self(), {:search, query})
    {:noreply, assign(socket, query: query, suggestions: [])}
  end
end

# lib/moldb/application.ex
defmodule Moldb.Application do
  use Application

  @impl true
  def start(_type, _args) do
    children = [
      MoldbWeb.Telemetry,
      Moldb.Repo,
      {DNSCluster, query: Application.get_env(:moldb, :dns_cluster_query) || :ignore},
      {Phoenix.PubSub, name: Moldb.PubSub},
      # Start the Finch HTTP client for sending emails
      {Finch, name: Moldb.Finch},
      # Start a worker by calling: Moldb.Worker.start_link(arg)
      # {Moldb.Worker, arg},
      # Start to serve requests, typically the last entry
      MoldbWeb.Endpoint,
      # Add SearchSupervisor to the supervision tree
      Moldb.SearchSupervisor
    ]

    opts = [strategy: :one_for_one, name: Moldb.Supervisor]
    Supervisor.start_link(children, opts)
  end

  @impl true
  def config_change(changed, _new, removed) do
    MoldbWeb.Endpoint.config_change(changed, removed)
    :ok
  end
end

# Placeholder for Search module (you'll need to implement this)
defmodule Moldb.Search do
  @moduledoc """
  Provides efficient search and suggestion functionality for compounds and substances.
  """

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

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

  @spec search(String.t(), integer(), integer()) :: [search_result()]
  def search(query, page, per_page) when is_binary(query) and is_integer(page) and is_integer(per_page) do
    offset = (page - 1) * per_page

    Task.async_stream([
      fn -> search_compounds(query, offset, per_page) end,
      fn -> search_substances(query, offset, per_page) end
    ], &(&1.()), max_concurrency: 2, timeout: 15_000)
    |> Enum.reduce([], fn {:ok, results}, acc -> acc ++ results end)
    |> Enum.sort_by(&rank(&1, query), :desc)
    |> Enum.take(per_page)
  end

  @spec suggest(String.t(), integer()) :: [String.t()]
  def suggest(query, limit) when is_binary(query) and is_integer(limit) do
    Task.async_stream([
      fn -> suggest_compounds(query, limit) end,
      fn -> suggest_substances(query, limit) end
    ], &(&1.()), max_concurrency: 2, timeout: 5_000)
    |> Enum.reduce([], fn {:ok, suggestions}, acc -> acc ++ suggestions end)
    |> Enum.sort_by(&String.jaro_distance(&1, query), :desc)
    |> Enum.take(limit)
  end

  defp search_compounds(query, offset, limit) do
    Compound
    |> where([c], ilike(c.iupac_name, ^"%#{query}%"))
    |> select([c], %{type: "compound", id: c.id, name: c.iupac_name, description: fragment("LEFT(?, 100)", c.description)})
    |> order_by([c], desc: fragment("SIMILARITY(?, ?)", c.iupac_name, ^query))
    |> limit(^limit)
    |> offset(^offset)
    |> Repo.all()
  end

  defp search_substances(query, offset, limit) do
    Substance
    |> where([s], ilike(s.substance_name, ^"%#{query}%"))
    |> select([s], %{type: "substance", id: s.id, name: s.substance_name, description: fragment("LEFT(?, 100)", s.description)})
    |> order_by([s], desc: fragment("SIMILARITY(?, ?)", s.substance_name, ^query))
    |> limit(^limit)
    |> offset(^offset)
    |> Repo.all()
  end

  defp suggest_compounds(query, limit) do
    Compound
    |> where([c], ilike(c.iupac_name, ^"#{query}%"))
    |> select([c], c.iupac_name)
    |> limit(^limit)
    |> Repo.all()
  end

  defp suggest_substances(query, limit) do
    Substance
    |> where([s], ilike(s.substance_name, ^"#{query}%"))
    |> select([s], s.substance_name)
    |> limit(^limit)
    |> Repo.all()
  end

  defp rank(%{type: type, name: name}, query) do
    base_score = if type == "compound", do: 1.0, else: 0.8
    similarity = String.jaro_distance(name, query)
    base_score * similarity
  end
end

Analyze and review this elixir code from phoenix framework web app using liveview. Refactor code for high efficiency and performance, scalability, concurrency and resilency. Follow SOLID, DRY programming principles and keep state minimal. Dont implement cache, indexes or any third party tools, libraries or software. Remove redundant code and unnecessary overhead. Ensure code consistency across codebase Provide robust solution.