defmodule Microwaveprop.Workers.IemreFetchWorkerTest do
  use Microwaveprop.DataCase, async: true

  alias Microwaveprop.Weather
  alias Microwaveprop.Weather.IemClient
  alias Microwaveprop.Weather.IemreObservation
  alias Microwaveprop.Workers.IemreFetchWorker

  @sample_iemre_data [
    %{"utc_hour" => 0, "p01m_mm" => 0.0, "skyc_pct" => 25.0},
    %{"utc_hour" => 1, "p01m_mm" => 0.5, "skyc_pct" => 50.0}
  ]

  describe "perform/1" do
    test "skips if IEMRE observation already exists" do
      Weather.upsert_iemre_observation(%{
        lat: 32.875,
        lon: -97.0,
        date: ~D[2026-03-28],
        hourly: @sample_iemre_data
      })

      # Stub should NOT be called
      Req.Test.stub(IemClient, fn conn ->
        Plug.Conn.send_resp(conn, 500, "Should not be called")
      end)

      job = %Oban.Job{
        args: %{
          "lat" => 32.875,
          "lon" => -97.0,
          "date" => "2026-03-28"
        }
      }

      assert :ok = IemreFetchWorker.perform(job)
      assert Repo.aggregate(IemreObservation, :count) == 1
    end

    test "fetches and stores IEMRE data on success" do
      Req.Test.stub(IemClient, fn conn ->
        Req.Test.json(conn, %{"data" => @sample_iemre_data})
      end)

      job = %Oban.Job{
        args: %{
          "lat" => 32.875,
          "lon" => -97.0,
          "date" => "2026-03-28"
        }
      }

      assert :ok = IemreFetchWorker.perform(job)
      assert Repo.aggregate(IemreObservation, :count) == 1

      obs = Repo.one(IemreObservation)
      assert obs.lat == 32.875
      assert obs.lon == -97.0
      assert obs.date == ~D[2026-03-28]
      assert length(obs.hourly) == 2
    end

    test "stores stub on empty data so backfill doesn't retry" do
      Req.Test.stub(IemClient, fn conn ->
        Req.Test.json(conn, %{"data" => []})
      end)

      job = %Oban.Job{
        args: %{
          "lat" => 32.875,
          "lon" => -97.0,
          "date" => "2026-03-28"
        }
      }

      assert :ok = IemreFetchWorker.perform(job)
      assert Repo.aggregate(IemreObservation, :count) == 1

      obs = Repo.one!(IemreObservation)
      assert obs.hourly == []
      assert obs.lat == 32.875
      assert obs.lon == -97.0
    end

    test "retries on transient server error" do
      Req.Test.stub(IemClient, fn conn ->
        Plug.Conn.send_resp(conn, 503, "Service Unavailable")
      end)

      job = %Oban.Job{
        args: %{
          "lat" => 32.875,
          "lon" => -97.0,
          "date" => "2026-03-28"
        }
      }

      assert {:error, "IEM IEMRE HTTP 503"} = IemreFetchWorker.perform(job)
    end

    test "cancels on permanent failure (404)" do
      Req.Test.stub(IemClient, fn conn ->
        Plug.Conn.send_resp(conn, 404, "not found")
      end)

      job = %Oban.Job{
        args: %{
          "lat" => 32.875,
          "lon" => -97.0,
          "date" => "2026-03-28"
        }
      }

      assert {:cancel, "IEM IEMRE HTTP 404"} = IemreFetchWorker.perform(job)
    end
  end

  describe "backoff/1" do
    test "uses exponential backoff capped at 6 hours" do
      assert IemreFetchWorker.backoff(%Oban.Job{attempt: 1}) == 120
      assert IemreFetchWorker.backoff(%Oban.Job{attempt: 2}) == 240
      assert IemreFetchWorker.backoff(%Oban.Job{attempt: 10}) == 21_600
      assert IemreFetchWorker.backoff(%Oban.Job{attempt: 20}) == 21_600
    end
  end
end
