defmodule Microwaveprop.ReleaseTest do
  @moduledoc """
  Smoke tests for the release-task entry points.

  Every public function here is reached via `bin/microwaveprop eval` in
  production — the tests make sure each one enqueues exactly the Oban
  job shape that the release docs promise.
  """
  use Microwaveprop.DataCase, async: false
  use Oban.Testing, repo: Microwaveprop.Repo

  alias Microwaveprop.Release
  alias Microwaveprop.Workers.AdminTaskWorker

  setup do
    # Oban runs inline in test, so AdminTaskWorker would actually execute.
    # Patch it out: we only care that Release *schedules* the right job.
    Oban.Testing.with_testing_mode(:manual, fn ->
      :ok
    end)
  end

  describe "backtest/1" do
    test "enqueues an AdminTaskWorker job with the feature name in args" do
      Oban.Testing.with_testing_mode(:manual, fn ->
        ExUnit.CaptureIO.capture_io(fn -> Release.backtest("naive_gradient") end)

        assert_enqueued(
          worker: AdminTaskWorker,
          args: %{task: "backtest", feature: "naive_gradient"}
        )
      end)
    end
  end

  describe "backtest_all/0" do
    test "enqueues the consolidated backtest task" do
      Oban.Testing.with_testing_mode(:manual, fn ->
        ExUnit.CaptureIO.capture_io(fn -> Release.backtest_all() end)
        assert_enqueued(worker: AdminTaskWorker, args: %{task: "backtest_all"})
      end)
    end
  end

  describe "climatology/1" do
    test "uses the default min_samples of 3" do
      Oban.Testing.with_testing_mode(:manual, fn ->
        ExUnit.CaptureIO.capture_io(fn -> Release.climatology() end)
        assert_enqueued(worker: AdminTaskWorker, args: %{task: "climatology", min_samples: 3})
      end)
    end

    test "passes through a custom min_samples" do
      Oban.Testing.with_testing_mode(:manual, fn ->
        ExUnit.CaptureIO.capture_io(fn -> Release.climatology(7) end)
        assert_enqueued(worker: AdminTaskWorker, args: %{task: "climatology", min_samples: 7})
      end)
    end
  end

  describe "recalibrate/0" do
    test "enqueues a recalibration" do
      Oban.Testing.with_testing_mode(:manual, fn ->
        ExUnit.CaptureIO.capture_io(fn -> Release.recalibrate() end)
        assert_enqueued(worker: AdminTaskWorker, args: %{task: "recalibrate"})
      end)
    end
  end

  describe "native_derive/1" do
    test "enqueues with the provided row limit" do
      Oban.Testing.with_testing_mode(:manual, fn ->
        ExUnit.CaptureIO.capture_io(fn -> Release.native_derive(2_500) end)
        assert_enqueued(worker: AdminTaskWorker, args: %{task: "native_derive", limit: 2_500})
      end)
    end

    test "defaults to a 10_000 limit" do
      Oban.Testing.with_testing_mode(:manual, fn ->
        ExUnit.CaptureIO.capture_io(fn -> Release.native_derive() end)
        assert_enqueued(worker: AdminTaskWorker, args: %{task: "native_derive", limit: 10_000})
      end)
    end
  end

  describe "scorer_diff/1" do
    test "prints a deprecation notice (propagation_scores is gone)" do
      output = ExUnit.CaptureIO.capture_io(fn -> Release.scorer_diff("{}") end)
      assert output =~ "scorer_diff is disabled"
    end
  end

  describe "native_backfill/1" do
    @tag :native_backfill
    test "exits cleanly when the contacts table is empty (no jobs to enqueue)" do
      Oban.Testing.with_testing_mode(:manual, fn ->
        output = ExUnit.CaptureIO.capture_io(fn -> Release.native_backfill(5) end)

        # Prints a summary header even when there's nothing to enqueue.
        assert output =~ "Enqueuing 0 native backfill jobs"
        assert output =~ "Done."
      end)
    end
  end
end
