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

  import Phoenix.LiveViewTest
  import Swoosh.TestAssertions
  import Berrypod.AccountsFixtures
  import Berrypod.OrdersFixtures

  setup do
    user_fixture()
    {:ok, _} = Berrypod.Settings.set_site_live(true)
    # drain the confirmation email user_fixture sends so it doesn't leak into assertions
    receive do
      {:email, _} -> :ok
    after
      0 -> :ok
    end

    :ok
  end

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

      assert html =~ "Get in touch"
      assert html =~ "Track your order"
    end

    test "shows order lookup form by default", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/contact")

      assert html =~ "Enter the email address you used at checkout"
      assert html =~ "Send link"
    end
  end

  describe "no-JS fallback" do
    test "order tracking form has action for no-JS submission", %{conn: conn} do
      {:ok, view, _html} = live(conn, ~p"/contact")

      assert has_element?(view, "form[action='/contact/lookup'][method='post']")
    end
  end

  describe "order lookup" do
    test "sends magic link when orders exist for email", %{conn: conn} do
      order_fixture(%{customer_email: "buyer@example.com", payment_status: "paid"})

      {:ok, view, _html} = live(conn, ~p"/contact")

      html = render_submit(view, "lookup_orders", %{"email" => "buyer@example.com"})

      assert html =~ "Check your inbox"
      assert html =~ "sent a link to your email address"
      assert_email_sent(to: "buyer@example.com", subject: "Your order lookup link")
    end

    test "shows not-found state for unknown email", %{conn: conn} do
      {:ok, view, _html} = live(conn, ~p"/contact")

      html = render_submit(view, "lookup_orders", %{"email" => "nobody@example.com"})

      assert html =~ "No orders found for that address"
      assert html =~ "Try again"
      assert_no_email_sent()
    end

    test "is case-insensitive for email lookup", %{conn: conn} do
      order_fixture(%{customer_email: "Buyer@Example.com", payment_status: "paid"})

      {:ok, view, _html} = live(conn, ~p"/contact")

      html = render_submit(view, "lookup_orders", %{"email" => "buyer@example.com"})

      assert html =~ "Check your inbox"
      # link is sent to the typed address, not the stored one
      assert_email_sent(to: "buyer@example.com")
    end

    test "ignores pending/failed orders", %{conn: conn} do
      order_fixture(%{customer_email: "buyer@example.com"})
      order_fixture(%{customer_email: "buyer@example.com", payment_status: "failed"})

      {:ok, view, _html} = live(conn, ~p"/contact")

      html = render_submit(view, "lookup_orders", %{"email" => "buyer@example.com"})

      assert html =~ "No orders found for that address"
      assert_no_email_sent()
    end

    test "reset button returns to idle form", %{conn: conn} do
      order_fixture(%{customer_email: "buyer@example.com", payment_status: "paid"})

      {:ok, view, _html} = live(conn, ~p"/contact")
      render_submit(view, "lookup_orders", %{"email" => "buyer@example.com"})

      html = render_click(view, "reset_tracking")

      assert html =~ "Enter the email address you used at checkout"
      assert html =~ "Send link"
    end
  end
end
