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

namespace :hiking do
  desc "Extract route=hiking relations (name/colour/network/operator, full geometry) from a local " \
       ".osm.pbf (e.g. one of PmtilesBuilder's own data/geofabrik_cache/*.osm.pbf) and write GeoJSON. " \
       "Prototype — see lib/maptool/hiking_trails.rb. Usage: rake hiking:trails[input.osm.pbf,out.geojson]"
  task :trails, %i[pbf out] do |_t, args|
    %i[pbf out].each { |key| abort("usage: rake hiking:trails[input.osm.pbf,out.geojson]") unless args[key] }

    require_relative "lib/maptool/hiking_trails"

    puts "Extracting hiking routes from #{args[:pbf]}..."
    geojson = Maptool::HikingTrails.extract(args[:pbf])
    File.write(args[:out], JSON.generate(geojson))
    puts "Wrote #{geojson["features"].size} route(s) to #{args[:out]}"
  end
end

namespace :gpx do
  desc "Backfill properties.id on Note-category features saved before edit/delete (and its id targeting) existed"
  task :backfill_note_ids do
    require "json"
    require "securerandom"

    data_dir = ENV.fetch("MAPTOOL_DATA_DIR") { File.join(__dir__, "data") }
    count = 0
    Dir.glob(File.join(data_dir, "gpx", "**", "*.geojson")).each do |file|
      # ".gpx.geojson"/".fit.geojson" are GpxStore's own cache sidecars for
      # an uploaded GPX/FIT (regenerated from the source on demand, see
      # GpxStore#geojson) — never hand-drawn notes, not worth touching.
      next if file.end_with?(".gpx.geojson", ".fit.geojson")

      collection = JSON.parse(File.read(file))
      changed = false
      (collection["features"] || []).each do |feature|
        properties = (feature["properties"] ||= {})
        next unless properties["category"] == "Note" && !properties["id"]

        properties["id"] = SecureRandom.uuid
        changed = true
        count += 1
      end
      File.write(file, JSON.generate(collection)) if changed
    end
    puts "Backfilled #{count} note id(s)"
  end
end
