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

  alias Berrypod.Orders
  alias Berrypod.Webhooks

  import Berrypod.ProductsFixtures
  import Berrypod.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
      Berrypod.Repo.delete_all(Berrypod.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

  # =============================================================================
  # Printful events
  # =============================================================================

  describe "handle_printful_event/2 — product events" do
    setup do
      conn = provider_connection_fixture(%{provider_type: "printful"})
      {:ok, printful_connection: conn}
    end

    test "product_updated triggers sync", %{printful_connection: _conn} do
      assert {:ok, %Oban.Job{}} =
               Webhooks.handle_printful_event("product_updated", %{})
    end

    test "product_synced triggers sync", %{printful_connection: _conn} do
      assert {:ok, %Oban.Job{}} =
               Webhooks.handle_printful_event("product_synced", %{})
    end

    test "product_deleted with sync_product id triggers delete" do
      assert {:ok, %Oban.Job{}} =
               Webhooks.handle_printful_event("product_deleted", %{
                 "sync_product" => %{"id" => 12345}
               })
    end

    test "product_deleted without id triggers full sync" do
      provider_connection_fixture(%{provider_type: "printful"})

      assert {:ok, %Oban.Job{}} =
               Webhooks.handle_printful_event("product_deleted", %{})
    end

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

    test "returns error when no printful connection" do
      # Delete the printful connection created in setup
      import Ecto.Query

      from(pc in Berrypod.Products.ProviderConnection,
        where: pc.provider_type == "printful"
      )
      |> Berrypod.Repo.delete_all()

      assert {:error, :no_connection} =
               Webhooks.handle_printful_event("product_updated", %{})
    end
  end

  describe "handle_printful_event/2 — order events" do
    setup do
      provider_connection_fixture(%{provider_type: "printful"})
      :ok
    end

    test "package_shipped sets tracking and shipped_at" do
      {order, _v, _p, _c} = submitted_order_fixture()

      assert {:ok, updated} =
               Webhooks.handle_printful_event("package_shipped", %{
                 "order" => %{"external_id" => order.order_number},
                 "shipment" => %{
                   "tracking_number" => "PF-TRACK-001",
                   "tracking_url" => "https://tracking.printful.com/PF-TRACK-001"
                 }
               })

      assert updated.fulfilment_status == "shipped"
      assert updated.tracking_number == "PF-TRACK-001"
      assert updated.tracking_url == "https://tracking.printful.com/PF-TRACK-001"
      assert updated.shipped_at != nil
    end

    test "order_failed sets failed status" do
      {order, _v, _p, _c} = submitted_order_fixture()

      assert {:ok, updated} =
               Webhooks.handle_printful_event("order_failed", %{
                 "order" => %{"external_id" => order.order_number},
                 "reason" => "Out of stock"
               })

      assert updated.fulfilment_status == "failed"
      assert updated.fulfilment_error == "Out of stock"
    end

    test "order_failed with no reason uses default message" do
      {order, _v, _p, _c} = submitted_order_fixture()

      assert {:ok, updated} =
               Webhooks.handle_printful_event("order_failed", %{
                 "order" => %{"external_id" => order.order_number}
               })

      assert updated.fulfilment_error == "Order failed at Printful"
    end

    test "order_canceled sets cancelled status" do
      {order, _v, _p, _c} = submitted_order_fixture()

      assert {:ok, updated} =
               Webhooks.handle_printful_event("order_canceled", %{
                 "order" => %{"external_id" => order.order_number}
               })

      assert updated.fulfilment_status == "cancelled"
    end

    test "package_shipped with external_id at top level" do
      {order, _v, _p, _c} = submitted_order_fixture()

      assert {:ok, updated} =
               Webhooks.handle_printful_event("package_shipped", %{
                 "external_id" => order.order_number,
                 "shipment" => %{
                   "tracking_number" => "PF-TRACK-002"
                 }
               })

      assert updated.fulfilment_status == "shipped"
      assert updated.tracking_number == "PF-TRACK-002"
    end

    test "order event with unknown external_id returns error" do
      assert {:error, :order_not_found} =
               Webhooks.handle_printful_event("package_shipped", %{
                 "order" => %{"external_id" => "SS-000000-NOPE"}
               })
    end

    test "order event with no external_id returns error" do
      assert {:error, :missing_external_id} =
               Webhooks.handle_printful_event("package_shipped", %{
                 "order" => %{"id" => 12345}
               })
    end
  end
end
