// API client — ENG_API, ENG Transfer.html'de tanımlı
const api = {
  get: async (path) => {
    const res = await fetch(`${ENG_API}${path}`, {
      headers: { "Accept": "application/json" }
    });
    if (!res.ok) throw new Error(`API error: ${res.status}`);
    return res.json();
  },
  post: async (path, body) => {
    const res = await fetch(`${ENG_API}${path}`, {
      method: "POST",
      headers: { "Content-Type": "application/json", "Accept": "application/json" },
      body: JSON.stringify(body)
    });
    if (!res.ok) {
      const err = await res.json().catch(() => ({}));
      throw new Error(err.error || `API error: ${res.status}`);
    }
    return res.json();
  }
};

// LocationDto → ANTALYA_LOCATIONS formatına dönüştür
const mapLocation = (l) => ({
  id: l.id,
  type: l.type === 0 ? "airport" : l.type === 1 ? "hotel" : "city",
  name: l.name,
  meta: l.metaDescription || "",
  code: l.code,
  apiId: l.id,
});

// VehicleDto → prototype RESULTS_DATA formatına dönüştür
const mapVehicle = (v) => ({
  id: v.id,
  cat: ["shuttle","taxi","vip","minivan"][v.vehicleType] || "taxi",
  catLabel: ["SHUTTLE","TAXI","VIP CAR","MINIVAN"][v.vehicleType] || "TAXI",
  name: v.name,
  model: v.model,
  pax: v.maxPassengers,
  bag: v.maxBaggage,
  doors: v.doors,
  priceTry: v.priceInTRY,
  featured: v.isFeatured,
  features: v.features || [],
  img: v.name,
  apiId: v.id,
});

// RouteDto → prototype ROUTES formatına dönüştür
const mapRoute = (r) => ({
  from: r.fromLocationName,
  to: r.toLocationName,
  duration: r.durationMinutes >= 60
    ? `${Math.floor(r.durationMinutes/60)}s ${r.durationMinutes%60}dk`
    : `${r.durationMinutes} dk`,
  distance: `${r.distanceKm} km`,
  priceTry: r.basePriceInTRY,
});

// ReviewDto → prototype REVIEWS formatına dönüştür
const mapReview = (r) => ({
  name: r.authorName,
  country: r.country,
  text: r.text,
  stars: r.stars,
  date: new Date(r.createdAt).toLocaleDateString("tr-TR", { month: "short", year: "numeric" }).toUpperCase(),
});

window.engApi = api;
window.mapLocation = mapLocation;
window.mapVehicle = mapVehicle;
window.mapRoute = mapRoute;
window.mapReview = mapReview;
