defmodule BerrypodWeb.Shop.Home do
  use BerrypodWeb, :live_view

  alias Berrypod.Pages

  @impl true
  def mount(_params, _session, socket) 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)

    {:ok, socket}
  end

  @impl true
  def render(assigns) do
    ~H"""
    <BerrypodWeb.PageRenderer.render_page {assigns} />
    """
  end
end
