// Sample data for Meridian Sales CRM (real-estate developer selling apartments).
// Currency amounts are stored as plain numbers; rendered via money()/moneyK().

const AGENTS = [
  { id: "sofia",  name: "Sofia Mendes",  initials: "SM", color: "#2F6B4F", role: "Sales agent" },
  { id: "marco",  name: "Marco Reis",    initials: "MR", color: "#B45309", role: "Sales agent" },
  { id: "lena",   name: "Lena Costa",    initials: "LC", color: "#0E62B8", role: "Sales agent" },
  { id: "diana",  name: "Diana Khoury",  initials: "DK", color: "#B81E63", role: "Sales manager" },
];

const DEVELOPMENTS = [
  { id: "marina",  name: "Marina Verde",    city: "Porto",    total: 48, available: 19, reserved: 9, sold: 20, priceFrom: 285000, blurb: "Waterfront 1–3 bed apartments with river views" },
  { id: "jardim",  name: "Jardim Alto",     city: "Lisbon",   total: 32, available: 11, reserved: 6, sold: 15, priceFrom: 340000, blurb: "Garden-courtyard residences in Campo de Ourique" },
  { id: "foz",     name: "Foz Residences",  city: "Cascais",  total: 18, available: 5,  reserved: 3, sold: 10, priceFrom: 620000, blurb: "Premium oceanfront homes & penthouses" },
];

const UNITS = [
  // Marina Verde
  { id: "u1",  code: "MV-A104", dev: "marina", floor: 1, type: "2 bed",     beds: 2, size: 78,  price: 312000, view: "Garden",     status: "Available" },
  { id: "u2",  code: "MV-A204", dev: "marina", floor: 2, type: "2 bed",     beds: 2, size: 81,  price: 328000, view: "River",      status: "Reserved" },
  { id: "u3",  code: "MV-B301", dev: "marina", floor: 3, type: "1 bed",     beds: 1, size: 54,  price: 285000, view: "Courtyard",  status: "Available" },
  { id: "u4",  code: "MV-B402", dev: "marina", floor: 4, type: "3 bed",     beds: 3, size: 112, price: 465000, view: "River",      status: "Available" },
  { id: "u5",  code: "MV-C501", dev: "marina", floor: 5, type: "Penthouse", beds: 3, size: 138, price: 690000, view: "River",      status: "Sold" },
  { id: "u6",  code: "MV-A105", dev: "marina", floor: 1, type: "1 bed",     beds: 1, size: 52,  price: 279000, view: "Garden",     status: "Reserved" },
  // Jardim Alto
  { id: "u7",  code: "JA-101",  dev: "jardim", floor: 1, type: "2 bed",     beds: 2, size: 84,  price: 365000, view: "Courtyard",  status: "Available" },
  { id: "u8",  code: "JA-203",  dev: "jardim", floor: 2, type: "3 bed",     beds: 3, size: 118, price: 498000, view: "Garden",     status: "Available" },
  { id: "u9",  code: "JA-302",  dev: "jardim", floor: 3, type: "2 bed",     beds: 2, size: 86,  price: 388000, view: "City",        status: "Reserved" },
  { id: "u10", code: "JA-401",  dev: "jardim", floor: 4, type: "Penthouse", beds: 3, size: 132, price: 720000, view: "City",        status: "Available" },
  { id: "u11", code: "JA-102",  dev: "jardim", floor: 1, type: "1 bed",     beds: 1, size: 56,  price: 340000, view: "Courtyard",  status: "Sold" },
  // Foz Residences
  { id: "u12", code: "FZ-201",  dev: "foz",    floor: 2, type: "3 bed",     beds: 3, size: 145, price: 880000,  view: "Ocean",     status: "Available" },
  { id: "u13", code: "FZ-301",  dev: "foz",    floor: 3, type: "Penthouse", beds: 4, size: 210, price: 1450000, view: "Ocean",     status: "Reserved" },
  { id: "u14", code: "FZ-102",  dev: "foz",    floor: 1, type: "2 bed",     beds: 2, size: 98,  price: 620000,  view: "Garden",    status: "Available" },
  { id: "u15", code: "FZ-202",  dev: "foz",    floor: 2, type: "3 bed",     beds: 3, size: 150, price: 910000,  view: "Ocean",     status: "Sold" },
];

const devName = (id) => (DEVELOPMENTS.find((d) => d.id === id) || {}).name || "—";
const unitByCode = (code) => UNITS.find((u) => u.code === code);

// Leads. budget = max budget; value = deal value (unit price if matched, else budget).
const LEADS = [
  { id: "l1",  name: "Ana Ferreira",     email: "ana.ferreira@gmail.com",    phone: "+351 912 884 201", source: "Idealista",  status: "Viewing",     owner: "sofia", dev: "marina", unit: "MV-A204", budget: 340000, value: 328000, score: 88, lastTouch: "2h ago",  created: "May 30" },
  { id: "l2",  name: "Tomás Silva",      email: "tsilva@outlook.com",        phone: "+351 936 110 558", source: "Website",    status: "New",         owner: "sofia", dev: "marina", unit: null,      budget: 300000, value: 300000, score: 41, lastTouch: "5h ago",  created: "Jun 1"  },
  { id: "l3",  name: "Marta Oliveira",   email: "marta.o@gmail.com",         phone: "+351 919 442 087", source: "Referral",   status: "Offer",       owner: "sofia", dev: "jardim", unit: "JA-203",  budget: 520000, value: 498000, score: 92, lastTouch: "1d ago",  created: "May 24" },
  { id: "l4",  name: "James Whitfield",  email: "j.whitfield@me.com",        phone: "+44 7700 900812",  source: "Portal (UK)", status: "Reservation", owner: "lena",  dev: "foz",    unit: "FZ-301",  budget: 1500000,value: 1450000,score: 95, lastTouch: "3h ago",  created: "May 18" },
  { id: "l5",  name: "Beatriz Nunes",    email: "bea.nunes@gmail.com",       phone: "+351 925 663 109", source: "Walk-in",    status: "Qualified",   owner: "marco", dev: "jardim", unit: null,      budget: 400000, value: 400000, score: 67, lastTouch: "6h ago",  created: "May 29" },
  { id: "l6",  name: "Carlos Pinto",     email: "carlos.pinto@sapo.pt",      phone: "+351 961 220 774", source: "Idealista",  status: "New",         owner: "marco", dev: "marina", unit: null,      budget: 290000, value: 290000, score: 38, lastTouch: "1d ago",  created: "Jun 1"  },
  { id: "l7",  name: "Sophie Laurent",   email: "sophie.l@gmail.com",        phone: "+33 6 12 45 88 90",source: "Portal (FR)", status: "Viewing",     owner: "lena",  dev: "foz",    unit: "FZ-201",  budget: 950000, value: 880000, score: 79, lastTouch: "4h ago",  created: "May 27" },
  { id: "l8",  name: "Ricardo Gomes",    email: "rgomes@gmail.com",          phone: "+351 933 558 012", source: "Referral",   status: "Closed",      owner: "sofia", dev: "marina", unit: "MV-C501", budget: 700000, value: 690000, score: 100,lastTouch: "2d ago",  created: "Apr 30" },
  { id: "l9",  name: "Inês Carvalho",    email: "ines.c@hotmail.com",        phone: "+351 914 887 330", source: "Website",    status: "Qualified",   owner: "sofia", dev: "jardim", unit: null,      budget: 380000, value: 380000, score: 58, lastTouch: "8h ago",  created: "May 31" },
  { id: "l10", name: "David Cohen",      email: "dcohen@gmail.com",          phone: "+972 50 234 1187", source: "Portal (IL)", status: "Offer",       owner: "lena",  dev: "jardim", unit: "JA-401",  budget: 760000, value: 720000, score: 84, lastTouch: "1d ago",  created: "May 22" },
  { id: "l11", name: "Joana Sousa",      email: "joana.sousa@gmail.com",     phone: "+351 968 003 442", source: "Walk-in",    status: "Viewing",     owner: "marco", dev: "marina", unit: "MV-B402", budget: 480000, value: 465000, score: 73, lastTouch: "5h ago",  created: "May 28" },
  { id: "l12", name: "Henrik Bauer",     email: "h.bauer@gmail.com",         phone: "+49 151 2334 9981",source: "Portal (DE)", status: "New",         owner: "marco", dev: "foz",    unit: null,      budget: 900000, value: 900000, score: 49, lastTouch: "11h ago", created: "Jun 1"  },
  { id: "l13", name: "Cláudia Reis",     email: "claudia.r@gmail.com",       phone: "+351 911 776 230", source: "Referral",   status: "Reservation", owner: "sofia", dev: "marina", unit: "MV-A105", budget: 290000, value: 279000, score: 90, lastTouch: "1d ago",  created: "May 20" },
  { id: "l14", name: "Paulo Antunes",    email: "p.antunes@sapo.pt",         phone: "+351 939 552 118", source: "Idealista",  status: "Lost",        owner: "lena",  dev: "jardim", unit: null,      budget: 350000, value: 350000, score: 22, lastTouch: "4d ago",  created: "May 12" },
];

const leadById = (id) => LEADS.find((l) => l.id === id);
const agentById = (id) => AGENTS.find((a) => a.id === id) || { name: "Unassigned", initials: "—", color: "#999" };

// Active lead for the detail screen
const ACTIVE_LEAD = {
  ...LEADS[0],
  notes: [
    { author: "Sofia Mendes", time: "Today, 09:12", text: "Called — very keen on the river view. Wants to bring partner to a second viewing this Saturday." },
    { author: "Sofia Mendes", time: "May 30, 16:40", text: "Came through Idealista. Budget ~€340k, selling current flat, no chain issues. Pre-approved with Millennium." },
  ],
  activity: [
    { kind: "view",  text: "Viewing booked for MV-A204 · Sat 10:00", time: "2h ago" },
    { kind: "call",  text: "Outbound call · 6 min", time: "Today 09:12" },
    { kind: "mail",  text: "Sent floor plan + price list", time: "Yesterday" },
    { kind: "lead",  text: "Lead created from Idealista", time: "May 30" },
  ],
};

const TASKS = [
  { id: "t1",  title: "Call Ana Ferreira — confirm Saturday viewing", lead: "l1",  owner: "sofia", time: "Today · 11:00", type: "call",    priority: "high", done: false },
  { id: "t2",  title: "Send Jardim Alto price list to Beatriz",       lead: "l5",  owner: "marco", time: "Today · 14:00", type: "mail",    priority: "med",  done: false },
  { id: "t3",  title: "Follow up offer — Marta Oliveira (JA-203)",    lead: "l3",  owner: "sofia", time: "Today · 16:30", type: "handshake",priority: "high", done: false },
  { id: "t4",  title: "Prepare reservation contract — James W.",      lead: "l4",  owner: "lena",  time: "Today · 17:00", type: "doc",     priority: "high", done: false },
  { id: "t5",  title: "Viewing — Sophie Laurent at Foz FZ-201",       lead: "l7",  owner: "lena",  time: "Tomorrow · 10:00", type: "calendar",priority: "med", done: false },
  { id: "t6",  title: "Chase deposit — Cláudia Reis (MV-A105)",        lead: "l13", owner: "sofia", time: "Tomorrow · 12:00", type: "payments",priority: "med", done: false },
  { id: "t7",  title: "Welcome call — Tomás Silva (new lead)",         lead: "l2",  owner: "sofia", time: "Today · 10:00", type: "call",    priority: "low",  done: true  },
  { id: "t8",  title: "Email floor plans — Henrik Bauer",              lead: "l12", owner: "marco", time: "Yesterday",      type: "mail",    priority: "low",  done: true  },
];

// Reservations & payments (reservation fee + deposit milestones)
const PAYMENTS = [
  { id: "RES-1042", lead: "l4",  unit: "FZ-301", kind: "Reservation fee", method: "Bank transfer", amount: 25000, status: "Paid",    due: "May 19", agent: "lena"  },
  { id: "RES-1042b",lead: "l4",  unit: "FZ-301", kind: "Deposit (10%)",   method: "Bank transfer", amount: 145000,status: "Pending", due: "Jun 9",  agent: "lena"  },
  { id: "RES-1038", lead: "l13", unit: "MV-A105",kind: "Reservation fee", method: "Card",          amount: 5000,  status: "Paid",    due: "May 21", agent: "sofia" },
  { id: "RES-1038b",lead: "l13", unit: "MV-A105",kind: "Deposit (10%)",   method: "Bank transfer", amount: 27900, status: "Overdue", due: "May 31", agent: "sofia" },
  { id: "RES-1035", lead: "l8",  unit: "MV-C501",kind: "Deposit (10%)",   method: "Bank transfer", amount: 69000, status: "Paid",    due: "May 4",  agent: "sofia" },
  { id: "RES-1035b",lead: "l8",  unit: "MV-C501",kind: "Completion",      method: "Bank transfer", amount: 621000,status: "Paid",    due: "Apr 30", agent: "sofia" },
  { id: "RES-1051", lead: "l1",  unit: "MV-A204",kind: "Reservation fee", method: "Card",          amount: 5000,  status: "Pending", due: "Jun 5",  agent: "sofia" },
  { id: "RES-1049", lead: "l11", unit: "MV-B402",kind: "Reservation fee", method: "Bank transfer", amount: 5000,  status: "Pending", due: "Jun 6",  agent: "marco" },
];

const METRICS = {
  newLeadsWeek: 27, newLeadsDelta: "+18%",
  viewingsWeek: 14,
  reservationsMonth: 6, reservationsValue: 2840000,
  pipelineValue: 4960000,
  funnel: { New: 27, Qualified: 16, Viewing: 11, Offer: 6, Reservation: 4, Closed: 3 },
  leadSpark: [12, 18, 14, 22, 19, 24, 27],
  reservationSpark: [1, 2, 1, 3, 2, 4, 6],
};

// Per-agent leaderboard for manager view
const LEADERBOARD = [
  { agent: "sofia", reservations: 3, value: 1297000, viewings: 9 },
  { agent: "lena",  reservations: 2, value: 2330000, viewings: 7 },
  { agent: "marco", reservations: 1, value: 465000,  viewings: 6 },
];

Object.assign(window, { AGENTS, DEVELOPMENTS, UNITS, LEADS, TASKS, PAYMENTS, METRICS, LEADERBOARD, ACTIVE_LEAD, devName, unitByCode, leadById, agentById });
