// Shared data for Departures.pk

const IMG = {
  hunza:     "https://images.unsplash.com/photo-1605649487212-47bdab064df7?w=1200&q=80&auto=format&fit=crop",
  skardu:    "https://images.unsplash.com/photo-1454496522488-7a8e488e8606?w=1200&q=80&auto=format&fit=crop",
  naran:     "https://images.unsplash.com/photo-1469474968028-56623f02e42e?w=1200&q=80&auto=format&fit=crop",
  kalam:     "https://images.unsplash.com/photo-1464822759023-fed622ff2c3b?w=1200&q=80&auto=format&fit=crop",
  kumrat:    "https://images.unsplash.com/photo-1604537529428-15bcbeecfe4d?w=1200&q=80&auto=format&fit=crop",
  sharan:    "https://images.unsplash.com/photo-1441974231531-c6227db76b6e?w=1200&q=80&auto=format&fit=crop",
  europe:    "https://images.unsplash.com/photo-1499856871958-5b9627545d1a?w=1200&q=80&auto=format&fit=crop",
  thailand:  "https://images.unsplash.com/photo-1506665531195-3566af2b4dfa?w=1200&q=80&auto=format&fit=crop",
  turkey:    "https://images.unsplash.com/photo-1524231757912-21f4fe3a7200?w=1200&q=80&auto=format&fit=crop",
  dubai:     "https://images.unsplash.com/photo-1512453979798-5ea266f8880c?w=1200&q=80&auto=format&fit=crop",
  usa:       "https://images.unsplash.com/photo-1496442226666-8d4d0e62e6e9?w=1200&q=80&auto=format&fit=crop",
  indonesia: "https://images.unsplash.com/photo-1537996194471-e657df975ab4?w=1200&q=80&auto=format&fit=crop",
  umrah:     "https://images.unsplash.com/photo-1591604129939-f1efa4d9f7fa?w=1200&q=80&auto=format&fit=crop",
  gilgit:    "https://images.unsplash.com/photo-1426604966848-d7adac402bff?w=1200&q=80&auto=format&fit=crop",
  hero:      "https://images.unsplash.com/photo-1604537529428-15bcbeecfe4d?w=2000&q=85&auto=format&fit=crop",
  editorial: "https://images.unsplash.com/photo-1516655855035-d5215bcb5604?w=1200&q=80&auto=format&fit=crop",
  cta:       "https://images.unsplash.com/photo-1464822759023-fed622ff2c3b?w=2000&q=85&auto=format&fit=crop",
  /* Sikh Yatra — sourced from Wikimedia Commons via Special:FilePath (stable redirects to the actual image). */
  nankana:   "https://commons.wikimedia.org/wiki/Special:FilePath/Gurdwara%20Nankana%20Sahib%20Janam%20Asthan.jpg?width=1200",
  kartarpur: "https://commons.wikimedia.org/wiki/Special:FilePath/Gurdwara%20Darbar%20Sahib%20Kartarpur.jpg?width=1200",
  panja:     "https://commons.wikimedia.org/wiki/Special:FilePath/Punja%20sahib%20gurdwara.jpg?width=1200",
  derasahib: "https://commons.wikimedia.org/wiki/Special:FilePath/Gurdwara%20Dera%20Sahib%20Lahore.jpg?width=1200",
  rorisahib: "https://commons.wikimedia.org/wiki/Special:FilePath/Gurdwara%20Rori%20Sahib%2C%20Eminabad.jpg?width=1200",
  sachasauda:"https://commons.wikimedia.org/wiki/Special:FilePath/Gurdwara%20Sacha%20Sauda%2C%20Farooqabad.jpg?width=1200",
  yatraHero: "https://commons.wikimedia.org/wiki/Special:FilePath/Gurdwara%20Darbar%20Sahib%20Kartarpur.jpg?width=1800",
};

const INTL_DEST = [
  { ix: "EU · 14D", code: "01", title: "Europe", tag: "Switzerland · France · Italy", price: 485000, img: IMG.europe },
  { ix: "TH·MY · 8D", code: "02", title: "Thailand & Malaysia", tag: "Beaches · Temples · City Life", price: 215000, img: IMG.thailand },
  { ix: "TR · 7D", code: "03", title: "Dubai, Baku & Türkiye", tag: "Luxury · Culture · Cappadocia", price: 265000, img: IMG.turkey },
  { ix: "US·CA · 12D", code: "04", title: "USA & Canada", tag: "Times Square · Niagara · Rockies", price: 695000, img: IMG.usa },
  { ix: "ID · 7D", code: "05", title: "Indonesia", tag: "Bali · Lombok · Komodo", price: 185000, img: IMG.indonesia },
  { ix: "SA · 10D", code: "06", title: "Umrah Pilgrims", tag: "Makkah · Madinah · Jeddah", price: 345000, img: IMG.umrah },
];

const PAK_DEST = [
  { ix: "GB · 7D", code: "01", title: "Hunza & Skardu", tag: "Karakoram · Attabad · Fairy Meadows", price: 65000, img: IMG.hunza },
  { ix: "PJ · 4D", code: "02", title: "Gilgit & Murree", tag: "Hill stations · pine forests", price: 28000, img: IMG.gilgit },
  { ix: "KP · 5D", code: "03", title: "Naran & Shogran", tag: "Saif-ul-Malook · Siri Paye", price: 34000, img: IMG.naran },
  { ix: "KP · 5D", code: "04", title: "Kalam & Malam Jabba", tag: "Swat Valley · ski resort", price: 36000, img: IMG.kalam },
  { ix: "KP · 4D", code: "05", title: "Kumrat Valley", tag: "Untouched forests · riverside camps", price: 32000, img: IMG.kumrat },
  { ix: "KP · 3D", code: "06", title: "Sharan Forest", tag: "Pine hills · stargazing camps", price: 24000, img: IMG.sharan },
];

const TOURS = [
  { title: "4 Days Swat, Kalam & Mahodand Lake", dur: "4 Days", tags: ["Private", "Hotel 4★"], rating: 4.9, reviews: 128, price: 45000, img: IMG.kalam },
  { title: "7 Days Hunza, Khunjerab & Attabad", dur: "7 Days", tags: ["Group", "Hotel 4★"], rating: 4.9, reviews: 342, price: 78000, img: IMG.hunza },
  { title: "6 Days Skardu, Shigar & Deosai", dur: "6 Days", tags: ["Private", "Hotel 5★"], rating: 5.0, reviews: 96, price: 95000, img: IMG.skardu },
  { title: "10 Days Cappadocia & Istanbul", dur: "10 Days", tags: ["Int'l", "By Air"], rating: 4.8, reviews: 214, price: 285000, img: IMG.turkey },
  { title: "5 Days Naran, Shogran & Siri Paye", dur: "5 Days", tags: ["Family", "Hotel 3★"], rating: 4.7, reviews: 188, price: 38000, img: IMG.naran },
  { title: "8 Days Bali Honeymoon Escape", dur: "8 Days", tags: ["Honeymoon"], rating: 4.9, reviews: 76, price: 225000, img: IMG.indonesia },
  { title: "5 Days Dubai City & Desert", dur: "5 Days", tags: ["Int'l", "Hotel 5★"], rating: 4.8, reviews: 302, price: 195000, img: IMG.dubai },
  { title: "14 Days Umrah Premium Package", dur: "14 Days", tags: ["Umrah", "5★ Haram view"], rating: 5.0, reviews: 421, price: 395000, img: IMG.umrah },
];

const REVIEWS = [
  { n: "Amina Hamid", d: "MAR 2026", r: 5, t: "Our Hunza trip was planned down to the last detail. The driver was patient, hotels had stunning views, and the guide knew every story about the valley. Sending my parents next month." },
  { n: "Asjid Tahir", d: "FEB 2026", r: 5, t: "Did a 10-day Türkiye package with my wife. Everything from the airport pickup in Istanbul to the hot-air balloon in Cappadocia went smoothly. The WhatsApp support was instant." },
  { n: "Qurat-ul-Ain", d: "JAN 2026", r: 5, t: "Booked Umrah for my mother and aunt. The hotel was a 3-minute walk to Haram and the coordination in Makkah was exceptional. They came back emotional and thankful." },
  { n: "Usman Fayyaz", d: "DEC 2025", r: 5, t: "Corporate retreat for 28 people to Skardu in winter. Logistics were flawless — flights, 4x4s, heated hotels. My CEO is already asking about next year." },
  { n: "Uzair Ullah", d: "NOV 2025", r: 5, t: "Kumrat 4-day camp trip was pure magic. Honest pricing, no hidden extras, and the local team actually cared. Will travel with Departures again." },
  { n: "Sana Rehman", d: "OCT 2025", r: 5, t: "Honeymoon in Bali — every dinner, every villa, every transfer was thought through. I didn't lift a finger. Worth every rupee." },
];

const CLIENT_LOGOS = [
  { name: "Unilever", kind: "serif" }, { name: "Nestlé", kind: "serif" },
  { name: "HBL", kind: "alt" }, { name: "Standard Chartered", kind: "serif" },
  { name: "Netsol", kind: "mono" }, { name: "Deloitte.", kind: "serif" },
  { name: "Changhong", kind: "alt" }, { name: "UBL", kind: "alt" },
  { name: "KEMU", kind: "mono" }, { name: "AIMC", kind: "mono" },
  { name: "DUHS", kind: "alt" }, { name: "LCCI", kind: "mono" },
];

const BADGES = [
  { t: "Govt. of Pakistan", s: "Registered", crest: "GoP" },
  { t: "IPO", s: "Trademark Secured", crest: "IPO" },
  { t: "SECP", s: "Regd. Company", crest: "S" },
  { t: "PATO", s: "Tour Operators Assoc.", crest: "P" },
  { t: "LCCI", s: "Chamber Member", crest: "L" },
];

// Mock pricing table — simulates backend /api/calculator/estimate
const RATES = {
  "hunza-skardu": { base: 12000, hotel: [0, 5000, 8000, 14000], transport: { shared: 4000, "private-sedan": 9000, "private-suv": 13000, coaster: 7000 }, meals: 2200, guide: 1500 },
  "gilgit-murree": { base: 6500, hotel: [0, 3000, 5500, 9000], transport: { shared: 2500, "private-sedan": 6500, "private-suv": 9500, coaster: 4500 }, meals: 1800, guide: 1200 },
  "naran-shogran": { base: 7500, hotel: [0, 3500, 6000, 10000], transport: { shared: 3000, "private-sedan": 7000, "private-suv": 10500, coaster: 5000 }, meals: 1900, guide: 1300 },
  "kalam": { base: 8200, hotel: [0, 3800, 6500, 10800], transport: { shared: 3200, "private-sedan": 7500, "private-suv": 11000, coaster: 5200 }, meals: 2000, guide: 1400 },
  "kumrat": { base: 7800, hotel: [0, 3400, 5900, 9800], transport: { shared: 3100, "private-sedan": 7200, "private-suv": 10800, coaster: 5100 }, meals: 2000, guide: 1400 },
  "europe": { base: 48000, hotel: [0, 18000, 26000, 42000], transport: { shared: 0, "private-sedan": 12000, "private-suv": 18000, coaster: 9000 }, meals: 8500, guide: 4500 },
  "turkey": { base: 28000, hotel: [0, 12000, 18000, 28000], transport: { shared: 0, "private-sedan": 8000, "private-suv": 12000, coaster: 6000 }, meals: 5500, guide: 3000 },
  "dubai": { base: 32000, hotel: [0, 14000, 22000, 36000], transport: { shared: 0, "private-sedan": 9000, "private-suv": 13000, coaster: 7000 }, meals: 5800, guide: 3200 },
  "indonesia": { base: 26000, hotel: [0, 11000, 17000, 27000], transport: { shared: 0, "private-sedan": 7500, "private-suv": 11000, coaster: 5500 }, meals: 5000, guide: 2800 },
  "umrah": { base: 35000, hotel: [0, 15000, 24000, 40000], transport: { shared: 3500, "private-sedan": 0, "private-suv": 0, coaster: 5500 }, meals: 6000, guide: 2500 },
};

const DEST_OPTS = [
  { value: "hunza-skardu", label: "Hunza & Skardu", code: "GIL" },
  { value: "gilgit-murree", label: "Gilgit & Murree", code: "MUR" },
  { value: "naran-shogran", label: "Naran & Shogran", code: "NRN" },
  { value: "kalam", label: "Kalam & Swat", code: "SWT" },
  { value: "kumrat", label: "Kumrat Valley", code: "KUM" },
  { value: "europe", label: "Europe (CH·FR·IT)", code: "EUR" },
  { value: "turkey", label: "Türkiye & Cappadocia", code: "IST" },
  { value: "dubai", label: "Dubai & Baku", code: "DXB" },
  { value: "indonesia", label: "Indonesia (Bali)", code: "DPS" },
  { value: "umrah", label: "Umrah Pilgrimage", code: "JED" },
];

// Simulates the backend call — resolves in 280-520ms
const mockEstimate = (body) => new Promise((resolve) => {
  const t = 280 + Math.random() * 240;
  setTimeout(() => {
    const r = RATES[body.destination] || RATES["hunza-skardu"];
    const styleMult = body.tripStyle === "budget" ? 0.85 : body.tripStyle === "luxury" ? 1.35 : 1.0;
    const tierIdx = body.hotelTier === 3 ? 1 : body.hotelTier === 5 ? 3 : 2;
    const transportPer = r.transport[body.transport] ?? r.transport.shared;
    const perDay = (r.base + r.hotel[tierIdx] + transportPer + r.meals + r.guide) * styleMult;
    const perPerson = Math.round(perDay * body.days / 500) * 500;
    const total = perPerson * body.travellers;
    resolve({
      currency: "PKR",
      perPerson,
      total,
      breakdown: {
        transport: Math.round(transportPer * body.days * styleMult * body.travellers / 100) * 100,
        hotel: Math.round(r.hotel[tierIdx] * body.days * styleMult * body.travellers / 100) * 100,
        meals: Math.round(r.meals * body.days * body.travellers / 100) * 100,
        guide: Math.round(r.guide * body.days * body.travellers / 100) * 100,
      },
      validUntil: "2026-05-19",
    });
  }, t);
});

const formatPKR = (amount) => `Rs. ${new Intl.NumberFormat('en-PK').format(amount)}`;

const WHATSAPP_NUMBER = "923094951551";

const resolveDestinationLabel = (destination) => {
  if (!destination) return "Not specified";
  const byValue = DEST_OPTS.find((d) => d.value === destination);
  if (byValue) return byValue.label;
  return destination;
};

const composeWhatsAppMessage = (payload = {}) => {
  const destinationLabel = resolveDestinationLabel(payload.destination);
  const lines = [
    "Assalam o Alaikum Departures.pk team,",
    "",
    `I want to ${payload.action || "plan a trip"}.`,
  ];

  if (payload.packageName) lines.push(`Package/Tour: ${payload.packageName}`);
  if (payload.destination) lines.push(`Destination: ${destinationLabel}`);
  if (payload.duration) lines.push(`Duration: ${payload.duration}`);
  if (payload.travellers) lines.push(`Travellers: ${payload.travellers}`);
  if (payload.tripStyle) lines.push(`Trip style: ${payload.tripStyle}`);
  if (payload.hotelTier) lines.push(`Hotel tier: ${payload.hotelTier}★`);
  if (payload.transport) lines.push(`Transport: ${payload.transport}`);
  if (payload.perPerson) lines.push(`Per person estimate: ${payload.perPerson}`);
  if (payload.totalEstimate) lines.push(`Total estimate: ${payload.totalEstimate}`);
  if (payload.transportCost) lines.push(`Transport cost: ${payload.transportCost}`);
  if (payload.hotelCost) lines.push(`Hotel cost: ${payload.hotelCost}`);
  if (payload.mealsCost) lines.push(`Meals cost: ${payload.mealsCost}`);
  if (payload.guideCost) lines.push(`Guide & Ops cost: ${payload.guideCost}`);
  if (payload.validUntil) lines.push(`Estimate valid until: ${payload.validUntil}`);
  if (payload.price) lines.push(`Quoted price: ${payload.price}`);
  if (payload.source) lines.push(`Source: ${payload.source}`);

  lines.push("");
  lines.push("Please share package details, inclusions, and best available dates.");
  return lines.join("\n");
};

const getWhatsAppUrl = (payload = {}) => {
  const text = composeWhatsAppMessage(payload);
  return `https://wa.me/${WHATSAPP_NUMBER}?text=${encodeURIComponent(text)}`;
};

const openWhatsAppForIntent = (payload = {}) => {
  const url = getWhatsAppUrl(payload);
  window.open(url, "_blank", "noopener,noreferrer");
};

Object.assign(window, {
  IMG,
  INTL_DEST,
  PAK_DEST,
  TOURS,
  REVIEWS,
  CLIENT_LOGOS,
  BADGES,
  DEST_OPTS,
  mockEstimate,
  formatPKR,
  WHATSAPP_NUMBER,
  getWhatsAppUrl,
  openWhatsAppForIntent,
});
