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

  alias Berrypod.{Orders, Pages}

  @impl true
  def mount(_params, session, socket) do
    email = session["order_lookup_email"]
    page = Pages.get_page("orders")

    socket =
      socket
      |> assign(:page_title, "Your orders")
      |> assign(:lookup_email, email)
      |> assign(:page, page)

    socket =
      if email do
        assign(socket, :orders, Orders.list_orders_by_email(email))
      else
        assign(socket, :orders, nil)
      end

    {:ok, socket}
  end

  @impl true
  def handle_params(_params, _uri, socket), do: {:noreply, socket}

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