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

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

  alias Berrypod.Pages
  alias Berrypod.Pages.Defaults
  alias Berrypod.Settings
  alias Berrypod.Settings.ThemeSettings
  alias Berrypod.Media
  alias Berrypod.Products
  alias Berrypod.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."
    )
  end

  def render("500.html", assigns) do
    render_error_page(
      assigns,
      "500",
      "Server error",
      "Something went wrong on our end. Please try again later."
    )
  end

  def render("429.html", assigns) do
    # 429 gets a bare themed page - just the message, no nav/footer/hero
    render_bare_error(assigns, "429", "Too many requests", "Please wait a moment and try again.")
  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
    site_live = safe_load(&Settings.site_live?/0) || false

    assigns =
      assigns
      |> Map.put(:error_code, error_code)
      |> Map.put(:error_title, error_title)
      |> Map.put(:error_description, error_description)
      |> Map.put(:site_live, site_live)

    if site_live do
      render_themed_error(assigns)
    else
      render_minimal_error(assigns)
    end
  end

  defp render_bare_error(assigns, error_code, error_title, error_description) do
    {theme_settings, generated_css} = load_theme_data()

    assigns =
      assigns
      |> Map.put(:error_code, error_code)
      |> Map.put(:error_title, error_title)
      |> Map.put(:error_description, error_description)
      |> Map.put(:theme_settings, theme_settings)
      |> Map.put(:generated_css, generated_css)

    ~H"""
    <!DOCTYPE html>
    <html lang="en">
      <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/shop.css"} />
        <style id="theme-css">
          <%= Phoenix.HTML.raw(@generated_css) %>
        </style>
      </head>
      <body>
        <div
          class="shop-root themed"
          data-mood={@theme_settings.mood}
          data-typography={@theme_settings.typography}
          data-shape={@theme_settings.shape}
          data-density={@theme_settings.density}
        >
          <main style="min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 2rem;">
            <p style="font-size: 4rem; font-weight: 700; margin: 0; color: var(--color-text);">
              {@error_code}
            </p>
            <h1 style="font-size: 1.5rem; font-weight: 600; margin: 0.5rem 0; color: var(--color-text);">
              {@error_title}
            </h1>
            <p style="font-size: 1rem; opacity: 0.6; color: var(--color-text);">
              {@error_description}
            </p>
          </main>
        </div>
      </body>
    </html>
    """
  end

  defp render_minimal_error(assigns) do
    ~H"""
    <!DOCTYPE html>
    <html lang="en">
      <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/admin.css"} />
      </head>
      <body>
        <main class="setup-page" style="padding-top: 6rem; text-align: center;">
          <p style="font-size: 3rem; font-weight: 700; margin: 0;">{@error_code}</p>
          <h1 style="font-size: 1.25rem; font-weight: 600; margin: 0.5rem 0;">{@error_title}</h1>
          <p style="font-size: 0.875rem; opacity: 0.6;">{@error_description}</p>
        </main>
      </body>
    </html>
    """
  end

  defp render_themed_error(assigns) do
    {theme_settings, generated_css} = load_theme_data()
    logo_image = safe_load(&Media.get_logo/0)
    header_image = safe_load(&Media.get_header/0)
    categories = safe_load(fn -> Products.list_categories() end) || []

    page = safe_load(fn -> Pages.get_page("error") end) || Defaults.for_slug("error")

    assigns =
      assigns
      |> Map.put(:theme_settings, theme_settings)
      |> Map.put(:site_name, safe_load(&Settings.site_name/0) || "Store Name")
      |> Map.put(:site_description, safe_load(&Settings.site_description/0) || "")
      |> Map.put(:generated_css, generated_css)
      |> Map.put(:logo_image, logo_image)
      |> Map.put(:header_image, header_image)
      |> Map.put(:categories, categories)
      |> Map.put(:mode, :shop)
      |> Map.put(:cart_items, [])
      |> Map.put(:cart_count, 0)
      |> Map.put(:cart_subtotal, "£0.00")
      |> Map.put(:page, page)
      |> Map.put(:header_nav_items, load_nav_items("header"))
      |> Map.put(:footer_nav_items, load_nav_items("footer"))

    # Load block data (e.g. products for featured_products block)
    extra = safe_load(fn -> Pages.load_block_data(page.blocks, assigns) end) || %{}
    assigns = Map.merge(assigns, extra)

    ~H"""
    <!DOCTYPE html>
    <html lang="en">
      <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/shop.css"} />
        <style id="theme-css">
          <%= Phoenix.HTML.raw(@generated_css) %>
        </style>
      </head>
      <body>
        <div
          class="shop-root themed"
          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}
        >
          <BerrypodWeb.PageRenderer.render_page {assigns} />
        </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, &BerrypodWeb.Endpoint.static_path/1)
            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

  # Load nav items from database, falling back to defaults if DB fails
  # (error pages might render when database is unavailable)
  defp load_nav_items(location) do
    case safe_load(fn -> Berrypod.Site.nav_items_for_shop(location) end) do
      items when is_list(items) and items != [] -> items
      _ -> default_nav_items(location)
    end
  end

  defp default_nav_items("header"), do: Berrypod.Site.default_header_nav()
  defp default_nav_items("footer"), do: Berrypod.Site.default_footer_nav()
end
