defmodule BerrypodWeb.Shop.CustomPageTest do
  use BerrypodWeb.ConnCase, async: false

  import Phoenix.LiveViewTest
  import Berrypod.AccountsFixtures

  alias Berrypod.Pages
  alias Berrypod.Pages.PageCache

  setup do
    PageCache.invalidate_all()
    # Clear redirect ETS cache to avoid stale entries from other tests
    if :ets.whereis(:redirects_cache) != :undefined, do: :ets.delete_all_objects(:redirects_cache)
    user = user_fixture()
    {:ok, _} = Berrypod.Settings.set_site_live(true)
    %{user: user}
  end

  describe "published custom page" do
    setup do
      {:ok, _} =
        Pages.create_custom_page(%{
          slug: "our-story",
          title: "Our story",
          meta_description: "Learn about us"
        })

      :ok
    end

    test "renders at /:slug", %{conn: conn} do
      {:ok, _view, html} = live(conn, "/our-story")
      assert html =~ "Our story"
    end

    test "sets page title", %{conn: conn} do
      {:ok, view, _html} = live(conn, "/our-story")
      assert page_title(view) =~ "Our story"
    end
  end

  describe "unpublished custom page" do
    setup do
      {:ok, _} =
        Pages.create_custom_page(%{
          slug: "draft-page",
          title: "Draft page",
          published: false
        })

      :ok
    end

    test "returns 404 for anonymous users", %{conn: conn} do
      assert_raise BerrypodWeb.NotFoundError, fn ->
        live(conn, "/draft-page")
      end
    end

    test "renders for admin users", %{conn: conn, user: user} do
      conn = log_in_user(conn, user)
      {:ok, _view, html} = live(conn, "/draft-page")
      assert html =~ "Draft page"
    end
  end

  describe "nonexistent page" do
    test "returns 404", %{conn: conn} do
      assert_raise BerrypodWeb.NotFoundError, fn ->
        live(conn, "/does-not-exist")
      end
    end
  end

  describe "system routes are unaffected" do
    test "/about still routes to Content LiveView", %{conn: conn} do
      {:ok, _view, html} = live(conn, "/about")
      assert html =~ "About"
    end

    test "/cart still routes to Cart LiveView", %{conn: conn} do
      {:ok, _view, html} = live(conn, "/cart")
      assert html =~ "cart"
    end
  end

  describe "custom page with blocks" do
    setup do
      {:ok, _} =
        Pages.create_custom_page(%{
          slug: "faq",
          title: "FAQ",
          blocks: [
            %{
              "id" => "blk_test1",
              "type" => "hero",
              "settings" => %{
                "title" => "Frequently asked questions",
                "description" => "Answers to common questions",
                "variant" => "page"
              }
            }
          ]
        })

      :ok
    end

    test "renders blocks", %{conn: conn} do
      {:ok, _view, html} = live(conn, "/faq")
      assert html =~ "Frequently asked questions"
    end
  end

  describe "page editor on custom pages" do
    setup do
      {:ok, _} =
        Pages.create_custom_page(%{slug: "editable", title: "Editable page"})

      :ok
    end

    test "editing works with edit toggle", %{conn: conn, user: user} do
      conn = log_in_user(conn, user)
      {:ok, view, _html} = live(conn, "/editable")

      # Editor sheet should be visible for admins
      assert has_element?(view, ".editor-panel")

      # Click the edit button in the sheet to enter edit mode
      view |> element(".editor-fab") |> render_click()

      # Now the editor sheet content should be visible (sheet state changes to open)
      assert has_element?(view, ".editor-panel-content")
    end
  end
end
