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

  Uses the email session cookie (30 days) set during order lookup
  verification or checkout completion.
  """

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

  alias Berrypod.{Orders, Pages}

  def init(socket, _params, _uri, session) do
    email = session["email_session"]
    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

    {:noreply, socket}
  end

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

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