defmodule SimpleshopTheme.WebhooksTest do
  use SimpleshopTheme.DataCase, async: false

  alias SimpleshopTheme.Orders
  alias SimpleshopTheme.Webhooks

  import SimpleshopTheme.ProductsFixtures
  import SimpleshopTheme.OrdersFixtures

  setup do
    conn = provider_connection_fixture(%{provider_type: "printify"})
    {:ok, provider_connection: conn}
  end

  describe "handle_printify_event/2 — product events" do
    test "product:updated triggers sync", %{provider_connection: _conn} do
      result =
        Webhooks.handle_printify_event(
          "product:updated",
          %{"id" => "123", "shop_id" => "456"}
        )

      assert {:ok, %Oban.Job{}} = result
    end

    test "product:publish:started triggers sync", %{provider_connection: _conn} do
      result =
        Webhooks.handle_printify_event(
          "product:publish:started",
          %{"id" => "123"}
        )

      assert {:ok, %Oban.Job{}} = result
    end

    test "product:deleted triggers delete", %{provider_connection: _conn} do
      result =
        Webhooks.handle_printify_event(
          "product:deleted",
          %{"id" => "123"}
        )

      assert {:ok, %Oban.Job{}} = result
    end

    test "shop:disconnected returns ok" do
      assert :ok = Webhooks.handle_printify_event("shop:disconnected", %{})
    end

    test "unknown event returns ok" do
      assert :ok = Webhooks.handle_printify_event("unknown:event", %{})
    end

    test "returns error when no provider connection" do
      SimpleshopTheme.Repo.delete_all(SimpleshopTheme.Products.ProviderConnection)

      assert {:error, :no_connection} =
               Webhooks.handle_printify_event(
                 "product:updated",
                 %{"id" => "123"}
               )
    end
  end

  describe "handle_printify_event/2 — order events" do
    test "order:sent-to-production updates fulfilment status" do
      {order, _v, _p, _c} = submitted_order_fixture()

      assert {:ok, updated} =
               Webhooks.handle_printify_event("order:sent-to-production", %{
                 "id" => "printify_abc",
                 "external_id" => order.order_number
               })

      assert updated.fulfilment_status == "processing"
      assert updated.provider_status == "in-production"
    end

    test "order:shipment:created sets tracking info and shipped_at" do
      {order, _v, _p, _c} = submitted_order_fixture()

      assert {:ok, updated} =
               Webhooks.handle_printify_event("order:shipment:created", %{
                 "id" => "printify_abc",
                 "external_id" => order.order_number,
                 "shipments" => [
                   %{
                     "tracking_number" => "1Z999AA1",
                     "tracking_url" => "https://ups.com/track/1Z999AA1"
                   }
                 ]
               })

      assert updated.fulfilment_status == "shipped"
      assert updated.tracking_number == "1Z999AA1"
      assert updated.tracking_url == "https://ups.com/track/1Z999AA1"
      assert updated.shipped_at != nil
    end

    test "order:shipment:delivered sets delivered_at" do
      {order, _v, _p, _c} = submitted_order_fixture()

      # First mark as shipped
      {:ok, order} =
        Orders.update_fulfilment(order, %{
          fulfilment_status: "shipped",
          shipped_at: DateTime.utc_now() |> DateTime.truncate(:second)
        })

      assert {:ok, updated} =
               Webhooks.handle_printify_event("order:shipment:delivered", %{
                 "id" => "printify_abc",
                 "external_id" => order.order_number
               })

      assert updated.fulfilment_status == "delivered"
      assert updated.delivered_at != nil
    end

    test "order event with unknown external_id returns error" do
      assert {:error, :order_not_found} =
               Webhooks.handle_printify_event("order:sent-to-production", %{
                 "id" => "printify_abc",
                 "external_id" => "SS-000000-NOPE"
               })
    end

    test "order event with missing external_id returns error" do
      assert {:error, :missing_external_id} =
               Webhooks.handle_printify_event("order:sent-to-production", %{
                 "id" => "printify_abc"
               })
    end
  end
end
