defmodule SimpleshopThemeWeb.ShopLive.About do
  use SimpleshopThemeWeb, :live_view

  alias SimpleshopTheme.Settings
  alias SimpleshopTheme.Media
  alias SimpleshopTheme.Theme.{CSSCache, CSSGenerator}

  @impl true
  def mount(_params, _session, socket) do
    theme_settings = Settings.get_theme_settings()

    generated_css =
      case CSSCache.get() do
        {:ok, css} ->
          css

        :miss ->
          css = CSSGenerator.generate(theme_settings)
          CSSCache.put(css)
          css
      end

    logo_image = Media.get_logo()
    header_image = Media.get_header()

    socket =
      socket
      |> assign(:page_title, "About")
      |> assign(:theme_settings, theme_settings)
      |> assign(:generated_css, generated_css)
      |> assign(:logo_image, logo_image)
      |> assign(:header_image, header_image)
      |> assign(:mode, :shop)
      |> assign(:cart_items, [])
      |> assign(:cart_count, 0)
      |> assign(:cart_subtotal, "£0.00")

    {:ok, socket}
  end

  @impl true
  def render(assigns) do
    ~H"""
    <SimpleshopThemeWeb.PageTemplates.about
      theme_settings={@theme_settings}
      logo_image={@logo_image}
      header_image={@header_image}
      mode={@mode}
      cart_items={@cart_items}
      cart_count={@cart_count}
      cart_subtotal={@cart_subtotal}
    />
    """
  end
end
