defmodule Towerops.JobMonitoring.MetricsTest do
  use Towerops.DataCase, async: true

  import Towerops.DevicesFixtures
  import Towerops.JobsFixtures

  alias Towerops.JobMonitoring.Metrics

  describe "calculate_all/0" do
    test "returns comprehensive metrics structure" do
      # Create device without SNMP to avoid auto-scheduling polling job
      device = device_fixture(%{snmp_enabled: false})

      # Executing jobs
      oban_job_fixture(%{
        worker: "Towerops.Workers.DevicePollerWorker",
        state: "executing",
        args: %{"device_id" => device.id},
        queue: "pollers"
      })

      # Queued jobs
      oban_job_fixture(%{
        worker: "Towerops.Workers.DiscoveryWorker",
        state: "scheduled",
        args: %{"device_id" => device.id},
        queue: "discovery"
      })

      # Completed jobs (last hour)
      now = DateTime.utc_now()

      oban_job_fixture(%{
        worker: "Towerops.Workers.DevicePollerWorker",
        state: "completed",
        args: %{"device_id" => device.id},
        completed_at: DateTime.add(now, -1800, :second),
        attempted_at: DateTime.add(now, -1810, :second),
        queue: "pollers"
      })

      metrics = Metrics.calculate_all()

      assert metrics.executing_count == 1
      assert metrics.queued_count == 1
      assert metrics.completed_last_hour == 1
      assert is_float(metrics.polling_success_rate_1h)
      assert is_map(metrics.queue_depths)
    end
  end
end
