defmodule Microwaveprop.Workers.PskrRecalibrationWorker do
  @moduledoc """
  Weekly cron that runs `Pskr.Recalibrator` against the
  PSKR calibration corpus.

  Scheduled for Sunday 04:00 UTC (off-peak, after the climatology
  rebuild and before the Monday-morning operator review window). At
  fire time the recalibrator decides whether the corpus is dense
  enough to bin — when it isn't, the run still records a row with
  status `"skipped_insufficient_data"` so the audit trail captures
  every fire.

  Manual reruns: enqueue this worker with no args. It always
  produces a fresh run record; previous runs stay queryable for
  trend comparison.
  """
  use Oban.Worker,
    queue: :backfill_enqueue,
    max_attempts: 3,
    unique: [
      period: 86_400,
      states: [:available, :scheduled, :executing, :retryable]
    ]

  alias Microwaveprop.Pskr.Recalibrator

  require Logger

  @impl Oban.Worker
  def perform(%Oban.Job{}) do
    run = Recalibrator.run()
    Logger.info("PskrRecalibrationWorker: status=#{run.status} sample_count=#{run.sample_count}")
    :ok
  end
end
