defmodule BerrypodWeb.OrderLookupControllerTest do
  use BerrypodWeb.ConnCase, async: false

  import Berrypod.AccountsFixtures
  import Berrypod.OrdersFixtures

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

  describe "POST /contact/lookup" do
    test "sends lookup email and redirects when orders exist", %{conn: conn} do
      order_fixture(%{customer_email: "buyer@test.com", payment_status: "paid"})

      conn = post(conn, ~p"/contact/lookup", %{"email" => "buyer@test.com"})

      assert redirected_to(conn) == "/contact"
      assert Phoenix.Flash.get(conn.assigns.flash, :info) =~ "sent a link"
    end

    test "shows error when no orders found", %{conn: conn} do
      conn = post(conn, ~p"/contact/lookup", %{"email" => "nobody@test.com"})

      assert redirected_to(conn) == "/contact"
      assert Phoenix.Flash.get(conn.assigns.flash, :error) =~ "No orders found"
    end

    test "shows error when email is missing", %{conn: conn} do
      conn = post(conn, ~p"/contact/lookup", %{})

      assert redirected_to(conn) == "/contact"
      assert Phoenix.Flash.get(conn.assigns.flash, :error) =~ "enter your email"
    end
  end
end
