defmodule BerrypodWeb.Shop.Pages.Home do
  @moduledoc """
  Home page handler for the unified Shop.Page LiveView.
  """

  import Phoenix.Component, only: [assign: 2, assign: 3]

  alias Berrypod.Pages

  def init(socket, _params, _uri) do
    page = Pages.get_page("home")
    extra = Pages.load_block_data(page.blocks, socket.assigns)

    base = BerrypodWeb.Endpoint.url()
    site_name = socket.assigns.site_name

    org_ld =
      Jason.encode!(
        %{
          "@context" => "https://schema.org",
          "@type" => "Organization",
          "name" => site_name,
          "url" => base <> "/"
        },
        escape: :html_safe
      )

    socket =
      socket
      |> assign(:page_title, "Home")
      |> assign(:og_url, base <> "/")
      |> assign(:json_ld, org_ld)
      |> assign(:page, page)
      |> assign(extra)

    {:noreply, socket}
  end

  def handle_params(_params, _uri, socket) do
    {:noreply, socket}
  end

  def handle_event(_event, _params, _socket), do: :cont
end
