defmodule WhoNeedHelp.Workers.LocalRetryProbe do
  @moduledoc """
  A side-effect-free worker used by the isolated local resilience profile.

  The first execution returns an expected error and the second succeeds. No
  application flow enqueues this worker; the resilience script requires an
  explicit confirmation value and removes its exact Oban row after collecting
  evidence.
  """

  use Oban.Worker, queue: :maintenance, max_attempts: 2, tags: ["local-resilience-probe"]

  @confirmation "isolated-local-resilience-probe"

  @impl Oban.Worker
  def perform(%Oban.Job{
        args: %{"confirmation" => @confirmation, "run_id" => run_id},
        attempt: 1
      })
      when is_binary(run_id) and run_id != "" do
    {:error, :expected_first_attempt_failure}
  end

  def perform(%Oban.Job{
        args: %{"confirmation" => @confirmation, "run_id" => run_id},
        attempt: attempt
      })
      when is_binary(run_id) and run_id != "" and attempt >= 2 do
    :ok
  end

  def perform(_job), do: {:cancel, :invalid_local_resilience_probe}

  @impl Oban.Worker
  def backoff(_job), do: 1
end
