defmodule Mix.Tasks.BuildingsFetch do
  @shortdoc "Bulk-download Microsoft Building Footprint tiles around DFW (DEV ONLY)"

  @moduledoc """
  Pre-warms the local Microsoft Building Footprints cache so per-Calculate
  requests never have to wait on a tile download.

      mix buildings.fetch                       # default: 500 mi radius around EM13
      mix buildings.fetch --lat 32.8 --lon -97 --radius-mi 500
      mix buildings.fetch --concurrency 8

  **Mix tasks do not run in production.** From an attached `iex` on the
  prod pod, call the underlying module directly:

      iex> Microwaveprop.Buildings.BulkFetch.run(32.8, -97.0, 500)
  """

  use Mix.Task

  alias Microwaveprop.Buildings.BulkFetch

  @default_lat 32.8
  @default_lon -97.0
  @default_radius_mi 500

  @switches [
    lat: :float,
    lon: :float,
    radius_mi: :integer,
    concurrency: :integer
  ]

  @impl Mix.Task
  def run(argv) do
    {opts, _, _} = OptionParser.parse(argv, switches: @switches)

    lat = Keyword.get(opts, :lat, @default_lat)
    lon = Keyword.get(opts, :lon, @default_lon)
    radius_mi = Keyword.get(opts, :radius_mi, @default_radius_mi)
    concurrency = Keyword.get(opts, :concurrency)

    Mix.Task.run("app.start")

    bulk_opts = if concurrency, do: [concurrency: concurrency], else: []
    result = BulkFetch.run(lat, lon, radius_mi, bulk_opts)

    Mix.shell().info("Done. #{inspect(result)}")
  end
end
