require "rake/testtask"

Rake::TestTask.new(:test) do |t|
  t.libs << "test" << "lib"
  t.test_files = FileList["test/test_*.rb"]
end

task default: :test

namespace :overpass do
  desc "Fetch camper-relevant POIs (water, dump stations, LPG, toilets, laundry, shops, springs) " \
       "for a bbox from Overpass and write GeoJSON. Usage: rake overpass:pois[south,west,north,east,out.geojson]"
  task :pois, [:south, :west, :north, :east, :out] do |_t, args|
    %i[south west north east out].each { |key| abort("usage: rake overpass:pois[south,west,north,east,out.geojson]") unless args[key] }

    require_relative "lib/maptool/overpass_pois"
    bbox = [args[:south], args[:west], args[:north], args[:east]].map(&:to_f)

    puts "Querying Overpass for #{bbox.join(",")}..."
    response = Maptool::OverpassPois.fetch(bbox)
    geojson = Maptool::OverpassPois.to_geojson(response)
    File.write(args[:out], JSON.generate(geojson))
    puts "Wrote #{geojson["features"].size} POIs to #{args[:out]}"
  end
end
