// Realistic-feeling fixture data for Pipal (Nepal social shopping).
// Currency NPR (Rs.). Cities: Kathmandu, Lalitpur, Bhaktapur, Pokhara.

const currency = (n) => "Rs " + Math.round(n).toLocaleString("en-IN");
const compactCurrency = (n) => {
  if (n >= 1e7) return "Rs " + (n/1e7).toFixed(2) + "Cr";
  if (n >= 1e5) return "Rs " + (n/1e5).toFixed(2) + "L";
  if (n >= 1e3) return "Rs " + (n/1e3).toFixed(1) + "k";
  return "Rs " + Math.round(n);
};
const compactNum = (n) => {
  if (n >= 1e6) return (n/1e6).toFixed(2) + "M";
  if (n >= 1e3) return (n/1e3).toFixed(1) + "k";
  return String(n);
};

// Deterministic pseudo-random
function seeded(seed) {
  let s = seed; return () => { s = (s * 9301 + 49297) % 233280; return s / 233280; };
}

function genSeries(n, seed, base = 100, vol = 0.2, drift = 0.005) {
  const r = seeded(seed);
  const out = []; let v = base;
  for (let i = 0; i < n; i++) {
    v = v * (1 + (r() - 0.5) * vol + drift);
    out.push(Math.max(0, v));
  }
  return out;
}

const REVENUE_14D = genSeries(14, 12, 480000, 0.18, 0.025);
const ORDERS_14D  = genSeries(14, 33, 1200, 0.16, 0.02);
const SIGNUPS_14D = genSeries(14, 41, 80, 0.35, 0.015);
const GMV_TODAY_HOURLY = genSeries(24, 7, 28000, 0.45, 0).map((v,i) =>
  // Simulate Nepal commerce day: low overnight, peaks at lunch + evening
  v * (0.2 + Math.exp(-Math.pow((i-13)/3.2, 2)) * 0.9 + Math.exp(-Math.pow((i-19.5)/2.2, 2)) * 1.1)
);

// --- Active orders / dispatch ---
const STORES = [
  { id: "STR-018", name: "Himali Organics",       owner: "Sushmita Pradhan",  city: "Kathmandu",    rating: 4.8, products: 142, status: "verified",    cat: "Grocery" },
  { id: "STR-104", name: "Boudha Bakehouse",      owner: "Tenzing Sherpa",     city: "Kathmandu",    rating: 4.7, products: 38,  status: "verified",    cat: "Bakery"  },
  { id: "STR-211", name: "Patan Threadworks",     owner: "Anita Shakya",       city: "Lalitpur",     rating: 4.6, products: 89,  status: "verified",    cat: "Apparel" },
  { id: "STR-256", name: "Newar Spice Co.",       owner: "Bikash Maharjan",    city: "Bhaktapur",    rating: 4.9, products: 64,  status: "verified",    cat: "Grocery" },
  { id: "STR-307", name: "Pokhara Press Coffee",  owner: "Diwas Gurung",       city: "Pokhara",      rating: 4.5, products: 22,  status: "verified",    cat: "Beverage"},
  { id: "STR-388", name: "Asan Atelier",          owner: "Rajani Tuladhar",    city: "Kathmandu",    rating: 4.4, products: 51,  status: "review",      cat: "Apparel" },
  { id: "STR-411", name: "Lakeside Linens",       owner: "Pemba Tamang",       city: "Pokhara",      rating: 4.6, products: 73,  status: "verified",    cat: "Home"    },
  { id: "STR-432", name: "Thamel Chai House",     owner: "Sabin Karki",        city: "Kathmandu",    rating: 4.3, products: 19,  status: "review",      cat: "Beverage"},
];

const DRIVERS = [
  { id: "DRV-021", name: "Bibek Adhikari",  vehicle: "motorcycle", area: "Kathmandu - Lazimpat",     status: "active",   trips: 412, rating: 4.92, online: true,  earnings: 184500, route: "RT-2210" },
  { id: "DRV-042", name: "Rohit Magar",     vehicle: "bicycle",    area: "Kathmandu - Thamel",       status: "active",   trips: 268, rating: 4.81, online: true,  earnings: 96300,  route: "RT-2208" },
  { id: "DRV-058", name: "Sunita Lama",     vehicle: "scooter",    area: "Lalitpur - Pulchowk",      status: "active",   trips: 503, rating: 4.95, online: true,  earnings: 211800, route: "RT-2212" },
  { id: "DRV-073", name: "Manish KC",       vehicle: "motorcycle", area: "Bhaktapur - Suryabinayak", status: "active",   trips: 188, rating: 4.74, online: true,  earnings: 78400,  route: "RT-2215" },
  { id: "DRV-089", name: "Pratiksha Joshi", vehicle: "scooter",    area: "Pokhara - Lakeside",       status: "active",   trips: 314, rating: 4.88, online: false, earnings: 142200, route: null },
  { id: "DRV-094", name: "Nabin Thapa",     vehicle: "motorcycle", area: "Kathmandu - Baneshwor",    status: "review",   trips: 0,   rating: null, online: false, earnings: 0,      route: null },
];

const ORDERS = [
  { id: "PPL-2104821", buyer: "Aayush Shrestha", store: "Himali Organics", store_id: "STR-018", items: 5, total: 2840, status: "in_transit",  driver: "DRV-021", eta: "12 min", placed: "10:42", city: "Kathmandu", lane: "Standard" },
  { id: "PPL-2104819", buyer: "Sneha Bajracharya", store: "Boudha Bakehouse", store_id: "STR-104", items: 2, total: 690,  status: "preparing",   driver: null,      eta: "—",      placed: "10:38", city: "Kathmandu", lane: "Flash" },
  { id: "PPL-2104815", buyer: "Rakesh Tamang",   store: "Newar Spice Co.",   store_id: "STR-256", items: 8, total: 4220, status: "ready",       driver: "DRV-058", eta: "queued", placed: "10:31", city: "Bhaktapur", lane: "Standard" },
  { id: "PPL-2104812", buyer: "Mira Rai",         store: "Patan Threadworks", store_id: "STR-211", items: 1, total: 3200, status: "delivered",   driver: "DRV-058", eta: "—",      placed: "09:55", city: "Lalitpur",  lane: "Economy" },
  { id: "PPL-2104808", buyer: "Sanjay Khadka",    store: "Pokhara Press Coffee", store_id: "STR-307", items: 3, total: 1180, status: "in_transit",  driver: "DRV-089", eta: "6 min",  placed: "09:48", city: "Pokhara",   lane: "Flash" },
  { id: "PPL-2104803", buyer: "Pratima Ghimire",  store: "Asan Atelier",      store_id: "STR-388", items: 2, total: 5400, status: "issue",       driver: "DRV-021", eta: "—",      placed: "09:32", city: "Kathmandu", lane: "Standard" },
  { id: "PPL-2104798", buyer: "Bishal Maharjan",  store: "Lakeside Linens",   store_id: "STR-411", items: 4, total: 7220, status: "ready",       driver: null,      eta: "queued", placed: "09:24", city: "Pokhara",   lane: "Standard" },
  { id: "PPL-2104796", buyer: "Anuska Pandey",    store: "Thamel Chai House", store_id: "STR-432", items: 6, total: 1840, status: "preparing",   driver: null,      eta: "—",      placed: "09:18", city: "Kathmandu", lane: "Flash" },
  { id: "PPL-2104790", buyer: "Saurav Lamichhane", store: "Himali Organics",  store_id: "STR-018", items: 11, total: 6340,status: "delivered",   driver: "DRV-042", eta: "—",      placed: "08:50", city: "Kathmandu", lane: "Standard" },
  { id: "PPL-2104785", buyer: "Kritika Basnet",   store: "Boudha Bakehouse",  store_id: "STR-104", items: 3, total: 940,  status: "delivered",   driver: "DRV-021", eta: "—",      placed: "08:40", city: "Kathmandu", lane: "Flash" },
];

// Routes with stops; coordinates are normalized 0..1 over a faux map.
const ROUTES = [
  {
    id: "RT-2210", driver: "Bibek Adhikari", driver_id: "DRV-021", lane: "Standard",
    started: "10:18", eta_done: "11:34", stops_done: 3, stops_total: 7,
    distance_km: 14.2, distance_done_km: 6.4, gmv: 18420,
    pin: { x: 0.46, y: 0.41 },
    stops: [
      { i: 1, label: "Himali Organics → Lazimpat",   t: "10:24", state: "done" },
      { i: 2, label: "Himali Organics → Naxal",      t: "10:33", state: "done" },
      { i: 3, label: "Boudha Bakehouse → Maharajgunj", t: "10:48", state: "done" },
      { i: 4, label: "Newar Spice Co. → Hadigaon",   t: "11:02", state: "current" },
      { i: 5, label: "Asan Atelier → Bhatbhateni",   t: "11:14", state: "queued" },
      { i: 6, label: "Asan Atelier → Chabahil",      t: "11:22", state: "queued" },
      { i: 7, label: "Thamel Chai → Thamel",         t: "11:32", state: "queued" },
    ],
  },
  {
    id: "RT-2208", driver: "Rohit Magar", driver_id: "DRV-042", lane: "Flash",
    started: "10:30", eta_done: "11:08", stops_done: 1, stops_total: 4,
    distance_km: 6.8, distance_done_km: 1.9, gmv: 4320,
    pin: { x: 0.31, y: 0.55 },
    stops: [
      { i: 1, label: "Boudha Bakehouse → Thamel",  t: "10:38", state: "done" },
      { i: 2, label: "Boudha Bakehouse → Jyatha",  t: "10:52", state: "current" },
      { i: 3, label: "Thamel Chai → Paknajol",     t: "11:00", state: "queued" },
      { i: 4, label: "Thamel Chai → Sorhakhutte",  t: "11:06", state: "queued" },
    ],
  },
  {
    id: "RT-2212", driver: "Sunita Lama", driver_id: "DRV-058", lane: "Standard",
    started: "10:42", eta_done: "12:20", stops_done: 2, stops_total: 6,
    distance_km: 11.5, distance_done_km: 3.8, gmv: 13260,
    pin: { x: 0.62, y: 0.66 },
    stops: [
      { i: 1, label: "Patan Threadworks → Pulchowk", t: "10:51", state: "done" },
      { i: 2, label: "Patan Threadworks → Jawalakhel", t: "11:04", state: "done" },
      { i: 3, label: "Newar Spice Co. → Kupondole",  t: "11:18", state: "current" },
      { i: 4, label: "Newar Spice Co. → Sanepa",     t: "11:32", state: "queued" },
      { i: 5, label: "Patan Threadworks → Lagankhel", t: "11:48", state: "queued" },
      { i: 6, label: "Newar Spice Co. → Satdobato",  t: "12:14", state: "queued" },
    ],
  },
  {
    id: "RT-2215", driver: "Manish KC", driver_id: "DRV-073", lane: "Standard",
    started: "10:55", eta_done: "12:08", stops_done: 0, stops_total: 5,
    distance_km: 9.4, distance_done_km: 0, gmv: 8910,
    pin: { x: 0.78, y: 0.40 },
    stops: [
      { i: 1, label: "Newar Spice → Suryabinayak",  t: "11:04", state: "current" },
      { i: 2, label: "Newar Spice → Thimi",         t: "11:20", state: "queued" },
      { i: 3, label: "Newar Spice → Nagadesh",      t: "11:34", state: "queued" },
      { i: 4, label: "Newar Spice → Kamalbinayak",  t: "11:52", state: "queued" },
      { i: 5, label: "Newar Spice → Dudhpati",      t: "12:04", state: "queued" },
    ],
  },
];

const PENDING_QUEUE = [
  { id: "PPL-2104819", store: "Boudha Bakehouse", drop: "Sanepa",    weight: "1.4 kg", lane: "Flash",    age: "4 min" },
  { id: "PPL-2104798", store: "Lakeside Linens",  drop: "Damside",   weight: "3.8 kg", lane: "Standard", age: "16 min" },
  { id: "PPL-2104796", store: "Thamel Chai House",drop: "Sorhakhutte", weight: "2.1 kg", lane: "Flash",  age: "22 min" },
  { id: "PPL-2104815", store: "Newar Spice Co.",  drop: "Kupondole", weight: "5.2 kg", lane: "Standard", age: "9 min" },
];

// KYC verification queue
const VERIFICATIONS = [
  { id: "KYC-9821", subject: "Tenzing Sherpa",   type: "Driver",   doc: "Citizenship", submitted: "12 min ago", risk: "low" },
  { id: "KYC-9819", subject: "Asan Atelier",     type: "Store",    doc: "PAN + VAT",   submitted: "47 min ago", risk: "med" },
  { id: "KYC-9817", subject: "Sabin Karki",      type: "Seller",   doc: "Citizenship", submitted: "1 hr ago",   risk: "low" },
  { id: "KYC-9812", subject: "Pemba Tamang",     type: "Driver",   doc: "License",     submitted: "2 hr ago",   risk: "low" },
];

// Top sellers
const TOP_STORES = STORES.slice(0, 6).map((s, i) => ({
  ...s,
  gmv_30d: 1840000 - i * 220000 + (i*i*30000),
  orders_30d: 1280 - i * 140,
  margin: 11.4 - i * 0.6,
  trend: genSeries(20, 100 + i, 100, 0.18, 0.01),
}));

// Build a city-grid of "delivery dots" for the dispatch map
const CITY_DOTS = (() => {
  const r = seeded(99);
  const dots = [];
  for (let i = 0; i < 80; i++) {
    dots.push({ x: r(), y: r(), kind: r() > 0.7 ? "pickup" : "drop" });
  }
  return dots;
})();

window.PipalData = {
  currency, compactCurrency, compactNum,
  REVENUE_14D, ORDERS_14D, SIGNUPS_14D, GMV_TODAY_HOURLY,
  STORES, DRIVERS, ORDERS, ROUTES, PENDING_QUEUE, VERIFICATIONS, TOP_STORES,
  CITY_DOTS,
};
