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

  import Phoenix.LiveViewTest
  import Berrypod.AccountsFixtures
  import Berrypod.OrdersFixtures

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

  defp with_lookup_email(conn, email) do
    conn
    |> Phoenix.ConnTest.init_test_session(%{})
    |> Plug.Conn.put_session("email_session", email)
  end

  describe "orders list — no session email" do
    test "shows expired link message", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/orders")

      assert html =~ "expired or is invalid"
    end
  end

  describe "orders list — with session email" do
    setup %{conn: conn} do
      %{conn: with_lookup_email(conn, "buyer@example.com")}
    end

    test "shows email address in header", %{conn: conn} do
      {:ok, _view, html} = live(conn, ~p"/orders")

      assert html =~ "buyer@example.com"
    end

    test "shows empty state when no paid orders", %{conn: conn} do
      order_fixture(%{customer_email: "buyer@example.com"})

      {:ok, _view, html} = live(conn, ~p"/orders")

      assert html =~ "No orders found for that email address"
    end

    test "lists paid orders for the session email", %{conn: conn} do
      order = order_fixture(%{customer_email: "buyer@example.com", payment_status: "paid"})

      {:ok, _view, html} = live(conn, ~p"/orders")

      assert html =~ order.order_number
      assert html =~ "Test product"
    end

    test "does not show other customers' orders", %{conn: conn} do
      other = order_fixture(%{customer_email: "other@example.com", payment_status: "paid"})

      {:ok, _view, html} = live(conn, ~p"/orders")

      refute html =~ other.order_number
    end

    test "links to order detail page", %{conn: conn} do
      order = order_fixture(%{customer_email: "buyer@example.com", payment_status: "paid"})

      {:ok, _view, html} = live(conn, ~p"/orders")

      assert html =~ ~p"/orders/#{order.order_number}"
    end
  end

  describe "order detail" do
    setup %{conn: conn} do
      %{conn: with_lookup_email(conn, "buyer@example.com")}
    end

    test "renders order detail for matching email", %{conn: conn} do
      order = order_fixture(%{customer_email: "buyer@example.com", payment_status: "paid"})

      {:ok, _view, html} = live(conn, ~p"/orders/#{order.order_number}")

      assert html =~ order.order_number
      assert html =~ "Test product"
    end

    test "redirects if order belongs to different email", %{conn: conn} do
      order = order_fixture(%{customer_email: "other@example.com", payment_status: "paid"})

      {:error, {:live_redirect, %{to: to}}} = live(conn, ~p"/orders/#{order.order_number}")

      assert to == ~p"/orders"
    end

    test "redirects if order is not paid", %{conn: conn} do
      order = order_fixture(%{customer_email: "buyer@example.com"})

      {:error, {:live_redirect, %{to: to}}} = live(conn, ~p"/orders/#{order.order_number}")

      assert to == ~p"/orders"
    end

    test "redirects if order number is unknown", %{conn: conn} do
      {:error, {:live_redirect, %{to: to}}} = live(conn, ~p"/orders/SS-UNKNOWN-0000")

      assert to == ~p"/orders"
    end

    test "shows tracking card when tracking number present", %{conn: conn} do
      order = order_fixture(%{customer_email: "buyer@example.com", payment_status: "paid"})

      {:ok, order} =
        Berrypod.Orders.update_fulfilment(order, %{
          fulfilment_status: "shipped",
          tracking_number: "RM123456789GB"
        })

      {:ok, _view, html} = live(conn, ~p"/orders/#{order.order_number}")

      assert html =~ "Shipment tracking"
      assert html =~ "RM123456789GB"
    end

    test "shows shipping address when present", %{conn: conn} do
      order =
        order_fixture(%{
          customer_email: "buyer@example.com",
          payment_status: "paid",
          shipping_address: %{
            "name" => "Jane Doe",
            "line1" => "42 Test Street",
            "city" => "London",
            "postal_code" => "SW1A 1AA",
            "country" => "GB"
          }
        })

      {:ok, _view, html} = live(conn, ~p"/orders/#{order.order_number}")

      assert html =~ "Jane Doe"
      assert html =~ "42 Test Street"
      assert html =~ "London"
    end
  end
end
