// Mocked API data for parkings near CDG and Orly.
// Simulates what a backend would return.

const PARKINGS = [
  {
    id: "cdg-01",
    airport: "CDG",
    name: "Horizon Parking CDG",
    type: "indoor",
    typeLabel: { fr: "Couvert", en: "Indoor" },
    rating: 4.7,
    reviews: 2841,
    distanceKm: 2.1,
    shuttleMin: 5,
    pricePerDay: 8.9,
    badges: ["cctv", "shuttle", "confirm"],
    services: ["shuttle", "cctv", "valet", "ev"],
    address: "12 rue des Aviateurs, 95700 Roissy-en-France",
    coords: { x: 62, y: 28 },
    recommended: true,
    images: 3,
  },
  {
    id: "cdg-02",
    airport: "CDG",
    name: "ParkCité Roissy Nord",
    type: "outdoor",
    typeLabel: { fr: "Extérieur", en: "Outdoor" },
    rating: 4.5,
    reviews: 1903,
    distanceKm: 3.6,
    shuttleMin: 8,
    pricePerDay: 6.5,
    badges: ["shuttle", "confirm"],
    services: ["shuttle", "cctv"],
    address: "3 avenue du Cargo, 95500 Le Mesnil-Amelot",
    coords: { x: 70, y: 40 },
    images: 2,
  },
  {
    id: "cdg-03",
    airport: "CDG",
    name: "Voiturier Premium T2",
    type: "valet",
    typeLabel: { fr: "Voiturier", en: "Valet" },
    rating: 4.9,
    reviews: 612,
    distanceKm: 0.4,
    shuttleMin: 0,
    pricePerDay: 14.9,
    badges: ["valet", "confirm", "premium"],
    services: ["valet", "cctv", "wash", "ev"],
    address: "Terminal 2F, Aéroport Roissy-CDG",
    coords: { x: 55, y: 22 },
    premium: true,
    images: 4,
  },
  {
    id: "cdg-04",
    airport: "CDG",
    name: "EcoPark Mitry",
    type: "outdoor",
    typeLabel: { fr: "Extérieur", en: "Outdoor" },
    rating: 4.3,
    reviews: 758,
    distanceKm: 5.2,
    shuttleMin: 12,
    pricePerDay: 4.9,
    badges: ["shuttle", "eco"],
    services: ["shuttle", "cctv", "ev"],
    address: "Route de Compans, 77290 Mitry-Mory",
    coords: { x: 78, y: 54 },
    images: 2,
  },
  {
    id: "cdg-05",
    airport: "CDG",
    name: "Gonesse Parc & Fly",
    type: "indoor",
    typeLabel: { fr: "Couvert", en: "Indoor" },
    rating: 4.6,
    reviews: 1244,
    distanceKm: 4.1,
    shuttleMin: 10,
    pricePerDay: 7.8,
    badges: ["cctv", "shuttle", "confirm"],
    services: ["shuttle", "cctv", "wash"],
    address: "5 rue de l'Aviation, 95500 Gonesse",
    coords: { x: 48, y: 52 },
    images: 3,
  },
  {
    id: "ory-01",
    airport: "ORY",
    name: "Azur Parking Orly Sud",
    type: "indoor",
    typeLabel: { fr: "Couvert", en: "Indoor" },
    rating: 4.8,
    reviews: 2107,
    distanceKm: 1.2,
    shuttleMin: 4,
    pricePerDay: 9.5,
    badges: ["cctv", "shuttle", "confirm", "premium"],
    services: ["shuttle", "cctv", "valet", "wash"],
    address: "8 rue du Pont des Halles, 94310 Orly",
    coords: { x: 42, y: 34 },
    recommended: true,
    images: 4,
  },
  {
    id: "ory-02",
    airport: "ORY",
    name: "ParkCité Orly Ouest",
    type: "outdoor",
    typeLabel: { fr: "Extérieur", en: "Outdoor" },
    rating: 4.4,
    reviews: 1388,
    distanceKm: 2.8,
    shuttleMin: 7,
    pricePerDay: 5.9,
    badges: ["shuttle", "confirm"],
    services: ["shuttle", "cctv"],
    address: "Avenue du Parc, 91320 Wissous",
    coords: { x: 60, y: 48 },
    images: 2,
  },
  {
    id: "ory-03",
    airport: "ORY",
    name: "Voiturier Express Orly",
    type: "valet",
    typeLabel: { fr: "Voiturier", en: "Valet" },
    rating: 4.9,
    reviews: 489,
    distanceKm: 0.3,
    shuttleMin: 0,
    pricePerDay: 13.5,
    badges: ["valet", "confirm", "premium"],
    services: ["valet", "cctv", "wash", "ev"],
    address: "Orly 4, Aéroport Paris-Orly",
    coords: { x: 35, y: 26 },
    premium: true,
    images: 3,
  },
  {
    id: "ory-04",
    airport: "ORY",
    name: "Chevilly Eco-Park",
    type: "outdoor",
    typeLabel: { fr: "Extérieur", en: "Outdoor" },
    rating: 4.2,
    reviews: 602,
    distanceKm: 4.5,
    shuttleMin: 11,
    pricePerDay: 4.5,
    badges: ["shuttle", "eco"],
    services: ["shuttle", "cctv", "ev"],
    address: "Rue des Peupliers, 94550 Chevilly-Larue",
    coords: { x: 70, y: 62 },
    images: 2,
  },
  {
    id: "ory-05",
    airport: "ORY",
    name: "Rungis Park & Go",
    type: "indoor",
    typeLabel: { fr: "Couvert", en: "Indoor" },
    rating: 4.5,
    reviews: 910,
    distanceKm: 3.3,
    shuttleMin: 9,
    pricePerDay: 7.2,
    badges: ["cctv", "shuttle", "confirm"],
    services: ["shuttle", "cctv", "wash"],
    address: "Marché de Rungis, 94150 Rungis",
    coords: { x: 55, y: 60 },
    images: 3,
  },
];

// Copy (FR / EN)
const COPY = {
  fr: {
    tagline: "Stationnez sereinement près de votre aéroport.",
    heroTitle: "Votre place de parking à\u00a0Paris-CDG et Orly",
    heroSub: "Réservez en 2 minutes. Confirmation immédiate et paiement sécurisé par Stripe.",
    airport: "Aéroport",
    from: "Arrivée au parking",
    to: "Retour au parking",
    search: "Rechercher",
    popularOffers: "Offres populaires",
    whyUs: "Pourquoi réserver avec nous",
    benefits: [
      { t: "Confirmation immédiate", d: "Votre place est réservée dès validation du paiement." },
      { t: "Paiement sécurisé par Stripe", d: "3D Secure, carte bancaire, Apple Pay et Google Pay." },
      { t: "Navette incluse", d: "Transfert gratuit vers les terminaux, 24h/24." },
    ],
    howItWorks: "Comment ça marche",
    steps: [
      { t: "Choisissez vos dates", d: "Indiquez votre aéroport et la durée du voyage." },
      { t: "Comparez les parkings", d: "Prix, services, avis clients et distance au terminal." },
      { t: "Réservez en ligne", d: "Confirmation immédiate par email avec QR code." },
    ],
    // Results
    resultsTitle: "parkings trouvés",
    sort: "Trier",
    sortOptions: ["Recommandé", "Prix croissant", "Distance", "Note"],
    filters: "Filtres",
    filterType: "Type de parking",
    filterServices: "Services",
    typeIndoor: "Couvert",
    typeOutdoor: "Extérieur",
    typeValet: "Voiturier",
    free: "Gratuit",
    perDay: "/ jour",
    total: "Total",
    book: "Réserver",
    viewDetails: "Voir le détail",
    // Detail
    about: "À propos du parking",
    servicesTitle: "Services inclus",
    location: "Localisation & accès",
    reviews: "Avis clients",
    reviewsCount: "avis",
    // Booking
    bookingTitle: "Finalisez votre réservation",
    yourStay: "Votre séjour",
    days: "jours",
    contact: "Vos coordonnées",
    firstName: "Prénom",
    lastName: "Nom",
    email: "Email",
    phone: "Téléphone",
    vehicle: "Votre véhicule",
    plate: "Immatriculation",
    model: "Marque et modèle",
    flight: "Numéro de vol (retour)",
    extras: "Options supplémentaires",
    payment: "Paiement",
    cardNumber: "Numéro de carte",
    cardExpiry: "Expiration",
    cardCvc: "CVC",
    payNow: "Payer et confirmer",
    summary: "Récapitulatif",
    // Confirmation
    confirmTitle: "Votre réservation est confirmée",
    confirmSub: "Un email avec votre QR code a été envoyé.",
    reference: "Référence",
    backHome: "Retour à l'accueil",
  },
  en: {
    tagline: "Park with peace of mind near your airport.",
    heroTitle: "Your parking spot at\u00a0Paris-CDG and Orly",
    heroSub: "Book in 2 minutes. Instant confirmation, secure payment by Stripe.",
    airport: "Airport",
    from: "Drop-off",
    to: "Pick-up",
    search: "Search",
    popularOffers: "Popular offers",
    whyUs: "Why book with us",
    benefits: [
      { t: "Instant confirmation", d: "Your spot is booked as soon as payment is validated." },
      { t: "Secure payment by Stripe", d: "3D Secure, all major cards, Apple Pay and Google Pay." },
      { t: "Shuttle included", d: "Free terminal transfer, 24/7." },
    ],
    howItWorks: "How it works",
    steps: [
      { t: "Pick your dates", d: "Tell us your airport and trip length." },
      { t: "Compare parkings", d: "Price, services, reviews and distance to terminal." },
      { t: "Book online", d: "Instant email confirmation with QR code." },
    ],
    resultsTitle: "parkings found",
    sort: "Sort",
    sortOptions: ["Recommended", "Price ascending", "Distance", "Rating"],
    filters: "Filters",
    filterType: "Parking type",
    filterServices: "Services",
    typeIndoor: "Indoor",
    typeOutdoor: "Outdoor",
    typeValet: "Valet",
    free: "Free",
    perDay: "/ day",
    total: "Total",
    book: "Book",
    viewDetails: "View details",
    about: "About this parking",
    servicesTitle: "Included services",
    location: "Location & access",
    reviews: "Customer reviews",
    reviewsCount: "reviews",
    bookingTitle: "Complete your booking",
    yourStay: "Your stay",
    days: "days",
    contact: "Your contact details",
    firstName: "First name",
    lastName: "Last name",
    email: "Email",
    phone: "Phone",
    vehicle: "Your vehicle",
    plate: "License plate",
    model: "Make and model",
    flight: "Return flight number",
    extras: "Optional extras",
    payment: "Payment",
    cardNumber: "Card number",
    cardExpiry: "Expiry",
    cardCvc: "CVC",
    payNow: "Pay and confirm",
    summary: "Summary",
    confirmTitle: "Your booking is confirmed",
    confirmSub: "An email with your QR code has been sent.",
    reference: "Reference",
    backHome: "Back to home",
  },
};

const SERVICE_LABELS = {
  fr: {
    shuttle: "Navette gratuite",
    cctv: "Vidéosurveillance 24/7",
    valet: "Voiturier",
    wash: "Lavage auto",
    ev: "Borne de recharge",
  },
  en: {
    shuttle: "Free shuttle",
    cctv: "CCTV 24/7",
    valet: "Valet service",
    wash: "Car wash",
    ev: "EV charging",
  },
};

const EXTRAS = {
  fr: [
    { id: "wash", label: "Lavage extérieur", price: 19 },
    { id: "fullclean", label: "Nettoyage complet", price: 49 },
    { id: "charge", label: "Recharge véhicule électrique", price: 15 },
    { id: "insurance", label: "Assurance Premium", price: 8 },
  ],
  en: [
    { id: "wash", label: "Exterior wash", price: 19 },
    { id: "fullclean", label: "Full detailing", price: 49 },
    { id: "charge", label: "EV charging", price: 15 },
    { id: "insurance", label: "Premium insurance", price: 8 },
  ],
};

Object.assign(window, { PARKINGS, COPY, SERVICE_LABELS, EXTRAS });

// --- DYNAMIC PRICING ---------------------------------------------------------
// Price = base × season × occupancy × weekend. Deterministic by (parkingId + fromDate).

// Month multiplier (1 = Jan, 12 = Dec).
// High: Jul, Aug, Dec (holidays) and Feb (school holidays).
// Low: Jan, Oct, Nov.
const SEASON_MULT = {
  1: 0.85, 2: 1.05, 3: 0.95, 4: 1.0, 5: 1.0, 6: 1.1,
  7: 1.30, 8: 1.35, 9: 1.0, 10: 0.85, 11: 0.85, 12: 1.25,
};

function seasonLabel(month, lang) {
  const m = SEASON_MULT[month] || 1;
  if (m >= 1.2) return lang === "fr" ? "Haute saison" : "Peak season";
  if (m >= 1.05) return lang === "fr" ? "Saison chargée" : "Busy season";
  if (m <= 0.9) return lang === "fr" ? "Basse saison" : "Low season";
  return lang === "fr" ? "Saison normale" : "Normal season";
}

// Deterministic hash — same parking + same date = same number [0..1).
function hashSeed(str) {
  let h = 2166136261;
  for (let i = 0; i < str.length; i++) {
    h ^= str.charCodeAt(i);
    h = Math.imul(h, 16777619);
  }
  return ((h >>> 0) % 10000) / 10000;
}

// Simulated occupancy for a given parking on a given day — correlated with season.
function computeOccupancy(parkingId, dateISO) {
  if (!dateISO) return 0.55;
  const d = new Date(dateISO);
  const seasonBias = (SEASON_MULT[d.getMonth() + 1] || 1) - 1; // -0.15 .. +0.35
  const noise = hashSeed(parkingId + dateISO);                 // 0..1
  // Base 45–70% with season bias pushing toward 90%+ in peak.
  const occ = 0.45 + noise * 0.25 + seasonBias * 0.9;
  return Math.max(0.15, Math.min(0.98, occ));
}

function occupancyMult(occ) {
  if (occ >= 0.95) return 1.6;
  if (occ >= 0.85) return 1.35;
  if (occ >= 0.7) return 1.18;
  if (occ >= 0.55) return 1.05;
  return 1.0;
}

function occupancyLabel(occ, lang) {
  if (occ >= 0.95) return lang === "fr" ? "Quasi complet" : "Almost full";
  if (occ >= 0.85) return lang === "fr" ? "Forte affluence" : "High demand";
  if (occ >= 0.7) return lang === "fr" ? "Affluence soutenue" : "Busy";
  if (occ >= 0.55) return lang === "fr" ? "Disponibilité modérée" : "Moderate availability";
  return lang === "fr" ? "Large disponibilité" : "Plenty of spots";
}

function weekendMult(fromDateISO) {
  if (!fromDateISO) return 1;
  const day = new Date(fromDateISO).getDay(); // 0 Sun, 5 Fri, 6 Sat
  return (day === 5 || day === 6) ? 1.05 : 1;
}

// Main API: returns { perDay, total, base, multipliers, occupancy, season }.
function computePricing(parking, search, days) {
  const base = parking.pricePerDay;
  const month = search?.from ? new Date(search.from).getMonth() + 1 : 6;
  const season = SEASON_MULT[month] || 1;
  const occupancy = computeOccupancy(parking.id, search?.from);
  const occMult = occupancyMult(occupancy);
  const wMult = weekendMult(search?.from);

  const perDay = Math.round(base * season * occMult * wMult * 10) / 10;
  const total = Math.round(perDay * days * 100) / 100;
  return {
    perDay, total, base, days,
    season, occupancy, occMult, wMult, month,
  };
}

Object.assign(window, {
  SEASON_MULT, seasonLabel, computeOccupancy, occupancyMult,
  occupancyLabel, weekendMult, computePricing,
});
