defmodule Berrypod.Webhooks.ProductDeleteWorkerTest do
  use Berrypod.DataCase
  use Oban.Testing, repo: Berrypod.Repo

  alias Berrypod.Webhooks.ProductDeleteWorker
  alias Berrypod.Products

  import Berrypod.ProductsFixtures

  describe "perform/1" do
    test "deletes product when found" do
      conn = provider_connection_fixture(%{provider_type: "printify"})

      {:ok, product, :created} =
        Products.upsert_product(conn, %{
          provider_product_id: "test-product-123",
          title: "Test Product",
          provider_data: %{}
        })

      assert Products.get_product(product.id) != nil

      assert :ok =
               perform_job(ProductDeleteWorker, %{
                 provider_product_id: "test-product-123"
               })

      assert Products.get_product(product.id) == nil
    end

    test "returns ok when product not found" do
      _conn = provider_connection_fixture(%{provider_type: "printify"})

      assert :ok =
               perform_job(ProductDeleteWorker, %{
                 provider_product_id: "nonexistent-product"
               })
    end

    test "cancels when no provider connection" do
      # No connection created
      assert {:cancel, :no_connection} =
               perform_job(ProductDeleteWorker, %{
                 provider_product_id: "test-product-123"
               })
    end
  end
end
