import "package:shared/graph.dart";
import "package:shelf/shelf.dart";

import "utils.dart";

Response getPath(Request request) {
  final startLat = double.tryParse(request.url.queryParameters["start_lat"] ?? "");
  final startLong = double.tryParse(request.url.queryParameters["start_lon"] ?? "");
  final endLat = double.tryParse(request.url.queryParameters["end_lat"] ?? "");
  final endLong = double.tryParse(request.url.queryParameters["end_lon"] ?? "");
  if (startLat == null || startLong == null || endLat == null || endLong == null) {
    return Response.badRequest(body: "Could not parse coordinates", headers: corsHeaders);
  }

  final start = (lat: startLat, long: startLong);
  final end = (lat: endLat, long: endLong);
  final path = findPath(start, end) ?? [];
  final jsonBody = encodeJsonList(path);
  return Response.ok(jsonBody, headers: corsHeaders);
}
