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

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

  alias Berrypod.Products

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

    pc = provider_connection_fixture()

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

    product_variant_fixture(%{product: print, title: "8x10", price: 1999})
    Products.recompute_cached_fields(print)

    shirt =
      product_fixture(%{
        provider_connection: pc,
        title: "Forest T-Shirt",
        category: "Apparel",
        on_sale: true
      })

    product_variant_fixture(%{
      product: shirt,
      title: "Large",
      price: 2999,
      compare_at_price: 3999
    })

    Products.recompute_cached_fields(shirt)

    %{print: print, shirt: shirt}
  end

  describe "Collection page" do
    test "renders collection page for /collections/all", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/collections/all")

      assert html =~ "All Products"
    end

    test "renders collection page for specific category", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/collections/art-prints")

      assert html =~ "Art Prints"
    end

    test "displays products", %{conn: conn, print: print} do
      {:ok, _view, html} = live(conn, ~p"/collections/all")

      assert html =~ print.title
    end

    test "displays category filter buttons", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/collections/all")

      assert html =~ "Art Prints"
      assert html =~ "Apparel"
    end

    test "displays sort dropdown", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/collections/all")

      assert html =~ "Featured"
      assert html =~ "Newest"
      assert html =~ "Price: Low to High"
      assert html =~ "Price: High to Low"
      assert html =~ "Name: A-Z"
      assert html =~ "Name: Z-A"
    end

    test "redirects to /collections/all for unknown category", %{conn: conn} do
      {:error, {:live_redirect, %{to: to, flash: flash}}} =
        live(conn, ~p"/collections/nonexistent")

      assert to == "/collections/all"
      assert flash["error"] == "Collection not found"
    end

    test "filters products by category", %{conn: conn, print: print, shirt: shirt} do
      {:ok, _view, html} = live(conn, ~p"/collections/art-prints")

      assert html =~ print.title
      refute html =~ shirt.title
    end
  end

  describe "Sorting" do
    test "sorts by price ascending", %{conn: conn} do
      {:ok, view, _html} = live(conn, ~p"/collections/all")

      html =
        view
        |> element("form[phx-change='sort_changed']")
        |> render_change(%{sort: "price_asc"})

      assert html =~ "Price: Low to High"
    end

    test "sorts by price descending", %{conn: conn} do
      {:ok, view, _html} = live(conn, ~p"/collections/all")

      html =
        view
        |> element("form[phx-change='sort_changed']")
        |> render_change(%{sort: "price_desc"})

      assert html =~ "Price: High to Low"
    end

    test "sorts by name ascending", %{conn: conn} do
      {:ok, view, _html} = live(conn, ~p"/collections/all")

      html =
        view
        |> element("form[phx-change='sort_changed']")
        |> render_change(%{sort: "name_asc"})

      assert html =~ "Name: A-Z"
    end

    test "sorts by name descending", %{conn: conn} do
      {:ok, view, _html} = live(conn, ~p"/collections/all")

      html =
        view
        |> element("form[phx-change='sort_changed']")
        |> render_change(%{sort: "name_desc"})

      assert html =~ "Name: Z-A"
    end

    test "sort parameter is preserved in URL", %{conn: conn} do
      {:ok, view, _html} = live(conn, ~p"/collections/all?sort=price_asc")

      html = render(view)

      assert html =~ "selected"
    end

    test "default sort is featured", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/collections/all")

      assert html =~ ~r/<option[^>]*value="featured"[^>]*selected/
    end
  end

  describe "Navigation" do
    test "category links preserve sort order", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/collections/all?sort=price_desc")

      assert html =~ "/collections/art-prints?sort=price_desc"
    end

    test "All button link preserves sort order", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/collections/art-prints?sort=name_asc")

      assert html =~ "/collections/all?sort=name_asc"
    end

    test "featured sort does not include query param in links", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/collections/all")

      assert html =~ ~s(href="/collections/art-prints")
      refute html =~ "/collections/art-prints?sort=featured"
    end
  end
end
