/* global React, ReactDOM */
const { useState, useEffect, useRef, useMemo } = React;

// ── Mock data ─────────────────────────────────────────────────────
const WORKSPACE = { brand: "CH Good Girl", market: "EU5", category: "Fragrance", quarter: "Q1 '26" };

const SIGNALS = [
  { room: "Listen", headline: "Refillable packaging becomes the dominant positive driver", meta: "TIKTOK · INSTAGRAM · +41% VOL · SENT +0.5", verb: "Investigate", spark: [2,3,2,4,5,4,6,7,9,8,10,12] },
  { room: "Personas", headline: "Eco-Mara would pay €95 with recycled glass — 4 of 6 synthetic personas agree", meta: "SIMULATED APR 18 · AUDIENCE 4.2M", verb: "Simulate again", spark: [4,4,4,5,5,6,5,6,7,7,8,8] },
  { room: "Predict", headline: "Olfactive layering enters growth stage in EU5 fragrance", meta: "SPATE · +38% YoY · LIKELY TO CONTINUE", verb: "Forecast", spark: [1,1,2,2,3,3,4,5,7,9,11,13] },
];

const TOPICS = [
  { n:"Refillable packaging", sub:"the driver of sentiment this week", mentions: 842, delta: +41, momentum: 92 },
  { n:"Fragrance longevity", sub:"comparison posts with Dior Sauvage", mentions: 613, delta: +12, momentum: 64 },
  { n:"Clean ingredients", sub:"ingredient transparency remains steady", mentions: 441, delta: +6, momentum: 48 },
  { n:"Gen-Z gifting", sub:"Valentine's afterglow on TikTok Spain", mentions: 327, delta: +18, momentum: 58 },
  { n:"Oud culture", sub:"plateauing after Q4 peak", mentions: 298, delta: 0, momentum: 24 },
  { n:"Celebrity endorsements", sub:"Carolina Herrera runway coverage", mentions: 210, delta: -4, momentum: 18 },
];

const POSTS = {
  "Refillable packaging": [
    { ch:"TikTok", au:"@beautyinsider_paris", t:"\"Absolutely love the refill system on CH Good Girl — the packaging is everything 🪩\"", s:+9.1 },
    { ch:"Instagram", au:"@skincarebymia", t:"\"Just bought the refill and honestly the weight of the glass bottle alone justifies the price\"", s:+8.4 },
    { ch:"Reddit", au:"r/fragrance", t:"\"The refill is €38, about half of a full bottle. That's actually a decent margin for a luxury house.\"", s:+6.8 },
    { ch:"TikTok", au:"@luxurywatchnews", t:"\"Refill system is nice but the cap still feels flimsy — scent lasts though\"", s:+3.1 },
    { ch:"X", au:"@fragheadz", t:"\"Why is refill only in Spain and France right now?\"", s:-1.2 },
  ],
  "Fragrance longevity": [
    { ch:"TikTok", au:"@scent_by_maya", t:"\"Good Girl lasts me 8 hours easy — the new refill formulation feels stronger\"", s:+7.6 },
    { ch:"Reddit", au:"r/fragrance", t:"\"Direct comparison with Sauvage: Good Girl wins on projection, loses on sillage\"", s:+5.2 },
  ],
  "Clean ingredients": [
    { ch:"Instagram", au:"@cleanbeautyedit", t:"\"The INCI list is posted publicly — appreciate the transparency\"", s:+6.0 },
  ],
  "Gen-Z gifting": [
    { ch:"TikTok", au:"@teenagerinparis", t:"\"my bf got me the mini CH Good Girl for valentine's 🥺 it's the cutest\"", s:+8.8 },
  ],
  "Oud culture": [
    { ch:"Reddit", au:"r/fragrance", t:"\"Oud is played out at this point, every house has a version\"", s:-4.1 },
  ],
  "Celebrity endorsements": [
    { ch:"X", au:"@fashionweekly", t:"\"Carolina Herrera runway was underwhelming this season\"", s:-3.0 },
  ],
};

const PERSONAS = [
  { id:"mara", name:"Eco-Conscious Mara", bio:"28 · Mid-high income · Barcelona",
    tags:["Sustainability","Clean beauty","Refill-first"],
    answer:"I'd consider it. The recycled glass aligns with my values and €95 is within my 'treat myself' budget. But I'd want to know: are the ingredients sustainably sourced? Is the supply chain transparent?" },
  { id:"ines", name:"Luxury Traditionalist Inés", bio:"52 · High income · Madrid",
    tags:["Heritage","Quality","Exclusivity"],
    answer:"I don't buy fragrance because it's recycled — I buy it because it lasts and smells like something I recognize. €95 is fair for Carolina Herrera. The packaging story doesn't move me either way." },
  { id:"zoe", name:"Gen-Z Trendsetter Zoé", bio:"23 · Mid income · Paris",
    tags:["TikTok","Value","Refill"],
    answer:"€95 is a lot for me but the refill brings it down long-term. I'd save for it if I saw three creators I trust talking about it. The glass thing is a plus but not the reason I'd buy." },
  { id:"khaled", name:"Metropolitan Khaled", bio:"35 · High income · Milano",
    tags:["Niche","Layering","Oud"],
    answer:"I buy niche. Good Girl is too mainstream for my vanity. If the refill was in a more exclusive packaging, maybe — but €95 for something this visible isn't where I spend." },
  { id:"anna", name:"Practical Anna", bio:"41 · Mid income · Berlin",
    tags:["Routine","Value","Gift-giving"],
    answer:"I'd buy this as a gift for my sister. For myself, €95 is above my regular fragrance budget. The recycled glass is a nice tiebreaker between two similar options." },
  { id:"sofia", name:"Discovery-Driven Sofia", bio:"31 · Mid-high · Lisbon",
    tags:["Samples","Reviews","Curiosity"],
    answer:"I'd want to sample first. The packaging sounds like something I'd photograph. At €95, the refill matters — it turns a one-time purchase into a relationship." },
];

const TRENDS = [
  { n:"Refillable luxury", x:0.72, y:0.55, size:54, state:"growth", delta:"+38%", accent:true },
  { n:"Olfactive layering", x:0.60, y:0.62, size:42, state:"growth", delta:"+22%" },
  { n:"Gender-fluid EDP", x:0.48, y:0.48, size:36, state:"growth", delta:"+14%" },
  { n:"Neurocosmetics", x:0.35, y:0.38, size:28, state:"early", delta:"+52%" },
  { n:"AI-personalized scent", x:0.22, y:0.30, size:22, state:"early", delta:"+78%" },
  { n:"Niche-to-mass", x:0.70, y:0.38, size:32, state:"growth", delta:"+9%" },
  { n:"Retail luxury", x:0.85, y:0.28, size:46, state:"mature", delta:"+2%" },
  { n:"Solid perfume", x:0.88, y:0.15, size:24, state:"mature", delta:"−3%" },
  { n:"Oud culture", x:0.80, y:0.35, size:30, state:"mature", delta:"0%" },
  { n:"Waterless", x:0.28, y:0.52, size:26, state:"early", delta:"+34%" },
];

// ── Looking Glass · Competitive Benchmark ─────────────────────
const COMPETITORS = [
  { brand: "CH Good Girl", house: "Puig", price: 92, share: 8.4, sov: 22, sent: 7.2, momentum: 92, self: true },
  { brand: "Dior Sauvage", house: "LVMH", price: 98, share: 12.1, sov: 28, sent: 6.8, momentum: 74 },
  { brand: "YSL Libre", house: "L'Oréal", price: 95, share: 9.2, sov: 18, sent: 6.1, momentum: 58 },
  { brand: "Chanel Chance", house: "Chanel", price: 105, share: 7.8, sov: 14, sent: 6.4, momentum: 48 },
  { brand: "Lancôme Idôle", house: "L'Oréal", price: 89, share: 5.3, sov: 10, sent: 5.8, momentum: 42 },
  { brand: "Gucci Bloom", house: "Kering", price: 94, share: 4.6, sov: 8, sent: 6.0, momentum: 38 },
];

const MOVES = [
  { date: "14 APR", brand: "Dior Sauvage", move: "Launched limited-edition refill bottle in Italy (€42 refill, €98 full).", impact: "high", verb: "Refill war" },
  { date: "09 APR", brand: "Chanel Chance", move: "Patent filing on magnetic refill closure in EU. Not yet shipped.", impact: "med", verb: "IP filing" },
  { date: "02 APR", brand: "YSL Libre", move: "Reduced Spain shelf price by 8% ahead of summer. Undercuts CH by €3.", impact: "high", verb: "Pricing" },
  { date: "28 MAR", brand: "Lancôme Idôle", move: "Mid-tier creator push on TikTok (12 creators, €85K estimated spend).", impact: "med", verb: "Creator" },
  { date: "19 MAR", brand: "Gucci Bloom", move: "New gift set for Mother's Day, €140, three EU5 markets.", impact: "low", verb: "NPD" },
];

// ── Ledger · Pricing Intelligence ──────────────────────────────
const PRICE_LADDER = [
  { sku: "CH Good Girl · 50ml EDP", price: 92, map: 88, street: 86, margin: 62, delta: -2.1, flag: "Amazon ES undercut" },
  { sku: "CH Good Girl · 80ml EDP", price: 128, map: 122, street: 122, margin: 68, delta: 0, flag: null },
  { sku: "CH Good Girl · 30ml Refill", price: 38, map: 38, street: 38, margin: 54, delta: 0, flag: null },
  { sku: "CH Good Girl · 50ml Refill", price: 52, map: 52, street: 48, margin: 58, delta: -7.7, flag: "Sephora FR discount" },
  { sku: "CH Good Girl · Gift Set SS'26", price: 148, map: 140, street: 140, margin: 71, delta: 0, flag: null },
];

// ── Postscript · Campaign Debrief ──────────────────────────────
const CAMPAIGNS = [
  { id: "gg-refill-es", name: "Good Girl Refill · Spain launch", window: "JAN–MAR '26", spend: 1.2, reach: 14.2, lift: 18, sent: 7.4, status: "Complete", verdict: "Outperformed" },
  { id: "gg-vday-eu", name: "Good Girl · Valentine's EU5", window: "FEB '26", spend: 0.8, reach: 9.6, lift: 11, sent: 7.1, status: "Complete", verdict: "On target" },
  { id: "gg-mothers", name: "Good Girl · Mother's Day IT", window: "APR '26", spend: 0.4, reach: 3.2, lift: 6, sent: 6.8, status: "Live", verdict: "—" },
];

// ── Materia · Ingredients Radar ────────────────────────────────
const INGREDIENTS = [
  { n: "Ambrocenide", family: "Synthetic musk", trend: "growth", momentum: 88, price: "+4%", supply: "Stable", novel: true },
  { n: "Iso E Super", family: "Woody amber", trend: "growth", momentum: 74, price: "+2%", supply: "Stable" },
  { n: "Cashmeran", family: "Woody musky", trend: "mature", momentum: 52, price: "−1%", supply: "Stable" },
  { n: "Hedione", family: "Jasmine-floral", trend: "mature", momentum: 44, price: "0%", supply: "Stable" },
  { n: "Galaxolide", family: "Synthetic musk", trend: "decline", momentum: 22, price: "−8%", supply: "Scrutiny", flag: "EU phase-out risk" },
  { n: "Oud (natural)", family: "Resinous", trend: "plateau", momentum: 32, price: "+18%", supply: "Tight", flag: "CITES concern" },
  { n: "Ambrette seed", family: "Natural musk", trend: "early", momentum: 68, price: "+12%", supply: "Scaling", novel: true },
  { n: "Biotech vetiver", family: "Woody earthy", trend: "early", momentum: 78, price: "+22%", supply: "Emerging", novel: true },
];

const BRIEF_BLOCKS = [
  { k:"Working title", v:"<em>CH Good Girl — The Refill Story</em>", ai:false },
  { k:"Context", v:"Refillable packaging is the dominant driver of sentiment for Good Girl in EU5. Refill launched in Spain/France in Jan '26. Italy and Germany are awaiting rollout.", ai:true },
  { k:"What the audience says", v:"Of 6 synthetic personas interviewed, 4 would pay €95 for recycled glass. The holdouts cite <em>niche preference</em> and <em>budget</em> — not rejection of the refill story.", ai:true },
  { k:"What Spate predicts", v:"Refillable luxury is in the <em>growth stage</em>, +38% YoY in EU5. Chanel filed a refill patent in Feb; Dior launched in Jan. Window to own the narrative in Italy: <em>Q2</em>.", ai:true },
  { k:"Recommendation", v:"Move Italy launch forward by six weeks. Seed with 8 mid-tier creators identified in Listen (engagement > 3.2%). Brief creators on 'the weight of glass' — not sustainability alone.", ai:true },
  { k:"Decision needed", v:"Italy launch date · creator budget (€180K ask) · packaging sample approval", ai:false },
];
