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

  import Phoenix.LiveViewTest
  import Berrypod.AccountsFixtures
  import Berrypod.ProductsFixtures

  setup do
    user_fixture()
    {:ok, _} = Berrypod.Settings.set_site_live(true)
    Berrypod.Site.seed_defaults()

    conn = provider_connection_fixture()

    product =
      product_fixture(%{
        provider_connection: conn,
        title: "Mountain Sunrise Print",
        category: "Art Prints"
      })

    product_variant_fixture(%{product: product, title: "8x10", price: 1999})

    # Recompute so cheapest_price is set
    Berrypod.Products.recompute_cached_fields(product)

    %{product: product}
  end

  describe "Home page" do
    test "renders the home page", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/")

      assert html =~ "Your headline goes here"
    end

    test "renders hero section with CTA", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/")

      assert html =~ "Shop the collection"
    end

    test "renders category navigation with real categories", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/")

      assert html =~ "Art Prints"
    end

    test "renders featured products section", %{conn: conn, product: product} do
      {:ok, _view, html} = live(conn, ~p"/")

      assert html =~ "Featured products"
      assert html =~ product.title
    end

    test "renders image and text section", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/")

      assert html =~ "Your story in a nutshell"
      assert html =~ "Read more about us"
    end

    test "renders header with shop name", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/")

      assert html =~ "Store Name"
    end

    test "renders footer with links", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/")

      assert html =~ ~s(href="/about")
      assert html =~ ~s(href="/contact")
    end
  end

  describe "Navigation links" do
    test "category links point to collections", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/")

      assert html =~ ~s(href="/collections/)
    end

    test "product links point to product pages", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/")

      assert html =~ ~s(href="/products/)
    end

    test "hero CTA links to collections", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/")

      assert html =~ ~s(href="/collections/all")
    end

    test "about link in image section", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/")

      assert html =~ ~s(href="/about")
    end
  end
end
