defmodule Microwaveprop.Workers.RoverMissionBackfillWorker do
  @moduledoc """
  Periodic safety net for the `/rover-planning` matrix. Walks every
  mission and re-enqueues `RoverPathProfileWorker` jobs for any
  `(rover_location × station)` pair whose `Path` is missing or not
  `:complete`. The path-profile worker short-circuits on `:complete`,
  so this is idempotent across runs.

  Recovers from transient failures (elevation API blips, rolling
  deploys that interrupted the original `create_mission` enqueue)
  without operator action.
  """

  use Oban.Worker,
    queue: :terrain,
    priority: 5,
    max_attempts: 3,
    unique: [period: 300, states: [:available, :scheduled, :executing, :retryable]]

  alias Microwaveprop.RoverPlanning

  @impl Oban.Worker
  def perform(%Oban.Job{}) do
    RoverPlanning.backfill_paths()
  end
end
