defmodule SimpleshopThemeWeb.ErrorHTML do
  @moduledoc """
  This module is invoked by your endpoint in case of errors on HTML requests.

  See config/config.exs.
  """
  use SimpleshopThemeWeb, :html

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

  def render("404.html", assigns) do
    render_error_page(
      assigns,
      "404",
      "Page Not Found",
      "Sorry, we couldn't find the page you're looking for. Perhaps you've mistyped the URL or the page has been moved."
    )
  end

  def render("500.html", assigns) do
    render_error_page(
      assigns,
      "500",
      "Server Error",
      "Something went wrong on our end. Please try again later or contact support if the problem persists."
    )
  end

  def render(template, _assigns) do
    Phoenix.Controller.status_message_from_template(template)
  end

  defp render_error_page(assigns, error_code, error_title, error_description) do
    # Load theme settings with fallback for error conditions
    {theme_settings, generated_css} = load_theme_data()
    logo_image = safe_load(&Media.get_logo/0)
    header_image = safe_load(&Media.get_header/0)

    products = safe_load(fn -> Products.list_visible_products(limit: 4) end) || []
    categories = safe_load(fn -> Products.list_categories() end) || []

    assigns =
      assigns
      |> Map.put(:theme_settings, theme_settings)
      |> Map.put(:generated_css, generated_css)
      |> Map.put(:logo_image, logo_image)
      |> Map.put(:header_image, header_image)
      |> Map.put(:products, products)
      |> Map.put(:categories, categories)
      |> Map.put(:error_code, error_code)
      |> Map.put(:error_title, error_title)
      |> Map.put(:error_description, error_description)
      |> Map.put(:mode, :shop)
      |> Map.put(:cart_items, [])
      |> Map.put(:cart_count, 0)
      |> Map.put(:cart_subtotal, "£0.00")

    ~H"""
    <!DOCTYPE html>
    <html lang="en" class="h-full">
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>{@error_code} - {@error_title}</title>
        <link phx-track-static rel="stylesheet" href={~p"/assets/css/app.css"} />
        <style id="theme-css">
          <%= Phoenix.HTML.raw(@generated_css) %>
        </style>
      </head>
      <body class="h-full">
        <div
          class="shop-root themed h-full"
          data-mood={@theme_settings.mood}
          data-typography={@theme_settings.typography}
          data-shape={@theme_settings.shape}
          data-density={@theme_settings.density}
          data-grid={@theme_settings.grid_columns}
          data-header={@theme_settings.header_layout}
          data-sticky={to_string(@theme_settings.sticky_header)}
          data-layout={@theme_settings.layout_width}
          data-shadow={@theme_settings.card_shadow}
        >
          <SimpleshopThemeWeb.PageTemplates.error
            theme_settings={@theme_settings}
            logo_image={@logo_image}
            header_image={@header_image}
            products={@products}
            categories={@categories}
            error_code={@error_code}
            error_title={@error_title}
            error_description={@error_description}
            mode={@mode}
            cart_items={@cart_items}
            cart_count={@cart_count}
            cart_subtotal={@cart_subtotal}
          />
        </div>
      </body>
    </html>
    """
  end

  defp load_theme_data do
    try 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

      {theme_settings, generated_css}
    rescue
      _ -> {%ThemeSettings{}, ""}
    end
  end

  defp safe_load(fun) do
    try do
      fun.()
    rescue
      _ -> nil
    end
  end
end
