defmodule BerrypodWeb.SetupHook do
  @moduledoc """
  Redirects to /setup when no admin account exists (fresh install).

  Add `{BerrypodWeb.SetupHook, :require_admin}` to any live_session
  that shouldn't be accessible before setup is complete.
  """

  import Phoenix.LiveView, only: [redirect: 2]

  def on_mount(:require_admin, _params, _session, socket) do
    if Berrypod.Accounts.has_admin?() do
      {:cont, socket}
    else
      {:halt, redirect(socket, to: "/setup")}
    end
  end
end
