// ─────────────────────────────────────────────────────────────
// Shared data, helpers, icons for the Folk prototype.
// ─────────────────────────────────────────────────────────────

// Stage ladder, closest → furthest. Used everywhere.
const STAGES = ['close', 'friend', 'acquaintance', 'stranger'];
const STAGE_META = {
  close:        { label: 'Close',        accent: '#D4960C', soft: 'rgba(212,150,12,0.10)',  ring: 'rgba(212,150,12,0.22)' },
  friend:       { label: 'Friend',       accent: '#5E8048', soft: 'rgba(94,128,72,0.10)',   ring: 'rgba(94,128,72,0.18)'  },
  acquaintance: { label: 'Acquaintance', accent: '#A8C5A0', soft: 'rgba(168,197,160,0.06)', ring: 'rgba(168,197,160,0.16)'},
  stranger:     { label: 'Stranger',     accent: '#586552', soft: 'transparent',            ring: 'rgba(88,101,82,0.14)'  },
};

// "Closer" stage relative to current
function nextStageUp(stage) {
  const i = STAGES.indexOf(stage);
  return i > 0 ? STAGES[i - 1] : null;
}

// Initial folk roster
const INITIAL_FOLK = [
  // CLOSE
  { handle: 'samh',     stage: 'close',        mutuals: 3, joined: 'Apr 2026', online: true,
    lastTouch: '12m', recentBlurb: 'ok pulling up to grind in 10', avatarSeed: ['#6B5018', '#3A2A07'] },
  { handle: 'marina_k', stage: 'close',        mutuals: 2, joined: 'Feb 2026', online: false,
    lastTouch: '3h',  recentBlurb: 'Driving down Friday at 4 — anyone want to caravan?', avatarSeed: ['#1B3A1D', '#76AB78'] },

  // FRIEND
  { handle: 'acorn-37', stage: 'friend',       mutuals: 2, joined: 'Mar 2026', online: true,
    lastTouch: '1d',  recentBlurb: 'Branched your BART tree', avatarSeed: ['#4F8E51', '#1B3A1D'] },
  { handle: 'oakling',  stage: 'friend',       mutuals: 1, joined: 'Mar 2026', online: false,
    lastTouch: '4d',  recentBlurb: 'Filed feedback on the BART site', avatarSeed: ['#5E8048', '#243319'] },
  { handle: 'birch-7',  stage: 'friend',       mutuals: 4, joined: 'Jan 2026', online: false,
    lastTouch: '6d',  recentBlurb: 'Slow morning at Phil Z',           avatarSeed: ['#7A4F2D', '#3A2A07'] },

  // ACQUAINTANCE
  { handle: 'fern-12',  stage: 'acquaintance', mutuals: 1, joined: 'Apr 2026', online: false,
    lastTouch: '2d',  recentBlurb: 'thanks for the rec', avatarSeed: ['#A8C5A0', '#1B3A1D'] },
  { handle: 'pine_92',  stage: 'acquaintance', mutuals: 2, joined: 'Feb 2026', online: true,
    lastTouch: '4h',  recentBlurb: 'Invited you to Indie Film', avatarSeed: ['#D4960C', '#3A2A07'] },
  { handle: 'tulip-aa', stage: 'acquaintance', mutuals: 0, joined: 'May 2026', online: false,
    lastTouch: '6h',  recentBlurb: 'Gave away symphony tickets', avatarSeed: ['#D6BFA8', '#7A4F2D'] },

  // STRANGER  — these only show up in search/pulse
  { handle: 'leaf-902', stage: 'stranger',     mutuals: 1, joined: 'May 2026', online: false,
    lastTouch: '34m', recentBlurb: 'Left a leaf on your branch', avatarSeed: ['#586552', '#243319'] },
];

// "Around now" — feeds the Pulse strip up top.
const AROUND_NOW = [
  { handle: 'samh',     verb: 'messaged', when: '12m' },
  { handle: 'pine_92',  verb: 'invited',  when: '4h'  },
  { handle: 'leaf-902', verb: 'leaf',     when: '34m' },
  { handle: 'marina_k', verb: 'planted',  when: '3h'  },
  { handle: 'acorn-37', verb: 'branched', when: '1d'  },
];

// Folk who appear in your forests but you don't have a stage relationship
// with yet — used by search to expand the "Other folk in your forests" tray.
const FOREST_FOLK = [
  { handle: 'redwood-04',  appearsIn: 'San Francisco',     mutualForestsCount: 1, avatarSeed: ['#3A2A07', '#7A4F2D'] },
  { handle: 'cedar-aa',    appearsIn: 'Indie Film',        mutualForestsCount: 1, avatarSeed: ['#1A2812', '#5E8048'] },
  { handle: 'maple_ix',    appearsIn: 'San Francisco',     mutualForestsCount: 1, avatarSeed: ['#445239', '#243319'] },
  { handle: 'willow-22',   appearsIn: 'Bay Area Cycling',  mutualForestsCount: 1, avatarSeed: ['#6B5018', '#3A2A07'] },
  { handle: 'sequoia',     appearsIn: 'San Francisco',     mutualForestsCount: 1, avatarSeed: ['#76AB78', '#1B3A1D'] },
];

// Folk findable across the whole network — no shared forests, no prior
// context. They surface in search under "Across the canopy" so people
// can add each other after meeting in person, even with zero overlap.
//
// Privacy posture: only handle + a generic location hint is visible.
// Tapping opens the ConnectSheet at stage Stranger, and the most
// natural verb is "Propose as Acquaintance" (the existing flow).
const ACROSS_CANOPY = [
  { handle: 'juniper_77', joined: 'Mar 2026', whereabouts: 'Portland · 0 mutual',  avatarSeed: ['#5E8048', '#1B3A1D'] },
  { handle: 'aspen-ix',   joined: 'Jan 2026', whereabouts: 'Brooklyn · 0 mutual',  avatarSeed: ['#7A4F2D', '#243319'] },
  { handle: 'mossy_22',   joined: 'May 2026', whereabouts: 'Austin · 0 mutual',    avatarSeed: ['#A8C5A0', '#445239'] },
];

// The signed-in user — used as "your handle" in Trade-handles.
const YOU = { handle: 'acorn-37', joined: 'Mar 2026', avatarSeed: ['#4F8E51', '#1B3A1D'] };

// Your existing groups (personal forests with ≥3 members) — for the
// "Add to a group" secondary sheet.
const YOUR_GROUPS = [
  { id: 'pch',     name: 'PCH weekend',       members: ['marina_k', 'samh'],         updated: '3h'  },
  { id: 'phil-z',  name: 'Phil Z mornings',   members: ['samh', 'birch-7'],          updated: '1d'  },
  { id: 'rudin',   name: 'Rudin study',       members: ['acorn-37', 'pine_92'],      updated: '4d'  },
];

// Forests you're in — for the "Invite to a forest" sheet.
// `kind` and `openness` mirror the existing Forests directory model.
const YOUR_FORESTS = [
  { id: 'sf',    name: 'San Francisco',     kind: 'city',     openness: 'open',   members: '4.1k' },
  { id: 'ucb',   name: 'UC Berkeley',       kind: 'campus',   openness: 'invite', members: '820'  },
  { id: 'film',  name: 'Indie Film',        kind: 'interest', openness: 'invite', members: '312'  },
  { id: 'cycle', name: 'Bay Area Cycling',  kind: 'interest', openness: 'open',   members: '186'  },
];

// Pre-baked "already in" mapping so Invite-to-forest can show
// which forests each user already inhabits.
const USER_IN_FORESTS = {
  samh:      ['sf'],
  marina_k:  ['sf'],
  'acorn-37':['sf', 'cycle'],
  oakling:   ['sf'],
  'birch-7': ['sf', 'film', 'cycle'],
  'fern-12': ['cycle'],
  pine_92:   ['film', 'sf'],
  'tulip-aa':[],
  'leaf-902':['sf'],
};

// Mutual-forest context (used inside ConnectSheet preview)
const MUTUAL_FORESTS = {
  samh:      ['San Francisco', 'PCH weekend', 'Phil Z mornings'],
  marina_k:  ['San Francisco', 'PCH weekend'],
  'acorn-37':['San Francisco', 'Bay Area Cycling'],
  oakling:   ['San Francisco'],
  'birch-7': ['San Francisco', 'Indie Film', 'Bay Area Cycling', 'Phil Z mornings'],
  'fern-12': ['Bay Area Cycling'],
  pine_92:   ['Indie Film', 'San Francisco'],
  'tulip-aa':[],
  'leaf-902':['San Francisco'],
};

// Recent trees per user (sample) — surfaced inside the sheet preview
const RECENT_TREES_BY_USER = {
  samh: [
    { c: 'Slow morning at Phil Z, anyone want to come work next to me. No pressure to talk.',
      forest: 'San Francisco', t: '20m' },
    { c: 'Driving down Friday at 4 — anyone want to caravan?',
      forest: 'PCH weekend', t: '3h', private: true },
  ],
  marina_k: [
    { c: 'Yes — the colors on the new BART system are way too similar. Yellow and orange?',
      forest: 'San Francisco', t: '47m' },
  ],
  'acorn-37': [
    { c: 'Anyone else find the new BART signs confusing? Took the wrong train this morning.',
      forest: 'San Francisco', t: '2h' },
  ],
  oakling: [
    { c: 'Filed feedback on the BART site, encouraging others to do the same.',
      forest: 'San Francisco', t: '50m' },
  ],
  'birch-7': [
    { c: 'Slow morning at Phil Z, anyone want to come work next to me.',
      forest: 'San Francisco', t: '6d' },
    { c: 'Hot take: the new Coen film is their best in a decade.',
      forest: 'Indie Film', t: '1w' },
  ],
  'fern-12': [
    { c: 'thanks for the rec! the velodrome is amazing',
      forest: 'Bay Area Cycling', t: '2d' },
  ],
  pine_92: [
    { c: 'Anyone going to the Roxie retrospective Friday night?',
      forest: 'Indie Film', t: '6h' },
  ],
  'tulip-aa': [
    { c: 'Got two free tickets to the symphony tonight, can\'t go. First DM gets them.',
      forest: 'San Francisco', t: '6h' },
  ],
  'leaf-902': [
    { c: 'Apparently the BART redesign was tested without color-blind users.',
      forest: 'San Francisco', t: '34m' },
  ],
};


// ─── Helpers ───────────────────────────────────────────────────

function FolkAvatar({ user, size = 36, online = false }) {
  const [a, b] = user.avatarSeed || ['#1B3A1D', '#243319'];
  return (
    <div style={{ position: 'relative', width: size, height: size, flexShrink: 0 }}>
      <div style={{
        width: size, height: size, borderRadius: '50%',
        background: `radial-gradient(circle at 30% 30%, ${a}, ${b})`,
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        font: `500 ${Math.round(size * 0.4)}px var(--font-mono)`,
        color: '#F2EDE4', letterSpacing: '-0.02em',
        border: '0.5px solid rgba(242,237,228,0.08)',
      }}>
        {user.handle[0].toUpperCase()}
      </div>
      {online && (
        <span style={{
          position: 'absolute', right: -1, bottom: -1,
          width: Math.max(8, size * 0.28), height: Math.max(8, size * 0.28),
          borderRadius: '50%', background: '#D4960C',
          boxShadow: '0 0 0 2px #0B1306',
        }}/>
      )}
    </div>
  );
}

// Stage chip — handles 'close' which the base system doesn't ship
function FolkStageBadge({ stage, size = 'sm' }) {
  if (stage === 'close') {
    return (
      <span style={{
        display: 'inline-flex', alignItems: 'center', gap: 5,
        padding: size === 'sm' ? '3px 9px' : '5px 12px',
        borderRadius: 999, background: '#3A2A07', border: '0.5px solid #6B5018',
        color: '#F4DCA0', font: `500 ${size === 'sm' ? 11 : 12}px var(--font-sans)`,
        whiteSpace: 'nowrap',
      }}>Close</span>
    );
  }
  return <StageBadge stage={stage} size={size}/>;
}


// ─── Icons (Lucide-derived) ─────────────────────────────────────

const PIcons = {
  MessageCircle: ({ size = 20, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color}
         strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
      <path d="M7.9 20A9 9 0 1 0 4 16.1L2 22z"/>
    </svg>
  ),
  UserPlus: ({ size = 20, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color}
         strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
      <path d="M2 21a8 8 0 0 1 13.292-6"/><circle cx="10" cy="8" r="5"/>
      <path d="M19 16v6"/><path d="M22 19h-6"/>
    </svg>
  ),
  Tree: ({ size = 20, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color}
         strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
      <path d="M12 22v-7l-2-2"/>
      <path d="M17 8v.8A6 6 0 0 1 13.8 20H10A6.5 6.5 0 0 1 7 8h.2A5.5 5.5 0 0 1 17 8"/>
      <path d="m14 14-2 2"/>
    </svg>
  ),
  ArrowUpRight: ({ size = 18, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color}
         strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
      <path d="M7 7h10v10"/><path d="M7 17 17 7"/>
    </svg>
  ),
  ChevronRight: ({ size = 18, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color}
         strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
      <path d="m9 18 6-6-6-6"/>
    </svg>
  ),
  X: ({ size = 18, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color}
         strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
      <path d="M18 6 6 18"/><path d="m6 6 12 12"/>
    </svg>
  ),
  Check: ({ size = 18, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color}
         strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
      <path d="M20 6 9 17l-5-5"/>
    </svg>
  ),
  // View toggles
  Ladder: ({ size = 18, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color}
         strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
      <path d="M6 3v18M18 3v18M6 8h12M6 14h12"/>
    </svg>
  ),
  Rings: ({ size = 18, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <circle cx="12" cy="12" r="9.5" stroke={color} strokeWidth="1.2" strokeOpacity="0.45"/>
      <circle cx="12" cy="12" r="6"  stroke={color} strokeWidth="1.3" strokeOpacity="0.7"/>
      <circle cx="12" cy="12" r="2.5" fill={color}/>
    </svg>
  ),
  Search: ({ size = 18, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color}
         strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
      <circle cx="11" cy="11" r="7"/><path d="m21 21-4.35-4.35"/>
    </svg>
  ),
  Plus: ({ size = 18, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color}
         strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round">
      <path d="M12 5v14M5 12h14"/>
    </svg>
  ),
  ChevronLeft: ({ size = 18, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color}
         strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
      <path d="m15 18-6-6 6-6"/>
    </svg>
  ),
  Lock: ({ size = 14, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color}
         strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
      <rect x="3" y="11" width="18" height="11" rx="2"/>
      <path d="M7 11V7a5 5 0 0 1 10 0v4"/>
    </svg>
  ),
  // For Trade handles
  Handshake: ({ size = 18, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color}
         strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
      <path d="m11 17 2 2a1 1 0 1 0 3-3"/>
      <path d="m14 14 2.5 2.5a1 1 0 1 0 3-3l-3.88-3.88a3 3 0 0 0-4.24 0l-.88.88a1 1 0 1 1-3-3l2.81-2.81a5.79 5.79 0 0 1 7.06-.87l.47.28a2 2 0 0 0 1.42.25L21 4"/>
      <path d="m21 3 1 11h-2"/><path d="M3 3 2 14l6.5 6.5a1 1 0 1 0 3-3"/>
      <path d="M3 4h8"/>
    </svg>
  ),
  Copy: ({ size = 14, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color}
         strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
      <rect x="8" y="8" width="14" height="14" rx="2"/>
      <path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/>
    </svg>
  ),
};


Object.assign(window, {
  STAGES, STAGE_META, nextStageUp,
  INITIAL_FOLK, AROUND_NOW, MUTUAL_FORESTS, RECENT_TREES_BY_USER,
  FOREST_FOLK, ACROSS_CANOPY, YOU,
  YOUR_GROUPS, YOUR_FORESTS, USER_IN_FORESTS,
  FolkAvatar, FolkStageBadge, PIcons,
});
