// LÜP Locations — interactive map of an example Sydney CBD + Eastern Suburbs network.
// Uses Leaflet (loaded via CDN in index.html) with CARTO Positron tiles.
window.LocationsSection = function LocationsSection() {
  const { audience } = React.useContext(window.AudienceContext);
  const mapRef = React.useRef(null);
  const mapInstanceRef = React.useRef(null);

  // Audience-aware header copy. Venues see the density/flywheel framing
  // (why coverage matters to them); customers see the everyday borrow/return promise.
  const header = audience === 'venue'
    ? {
        leadText: 'A dense network that drives the ',
        accentText: 'reuse flywheel.',
        subText: 'Every new venue strengthens the loop. Denser networks mean more drop-offs, faster cycles, and more bowls back in circulation — for every venue on the network.',
      }
    : {
        leadText: 'Borrow and return your bowl. ',
        accentText: 'Anywhere.',
        subText: 'Drop your LÜP bowl at any participating venue. No appointment, no app, no fuss.',
      };

  // Example end-state network — densely populated venues across Sydney CBD and the
  // Eastern Suburbs. Multiple venues per suburb to convey real network coverage.
  const venues = [
    // ── CBD ──
    { name: 'Stable Coffee', suburb: 'The Rocks', type: 'Café', lat: -33.8587, lng: 151.2087 },
    { name: 'Argyle Lane Bowls', suburb: 'The Rocks', type: 'Restaurant', lat: -33.8595, lng: 151.2089 },
    { name: 'Quay Street Eats', suburb: 'Circular Quay', type: 'Restaurant', lat: -33.8615, lng: 151.2113 },
    { name: 'Wharf Provisions', suburb: 'Circular Quay', type: 'Café', lat: -33.8617, lng: 151.2106 },
    { name: 'Crown Coffee Co.', suburb: 'Barangaroo', type: 'Café', lat: -33.8619, lng: 151.2008 },
    { name: 'Hickson Rd Café', suburb: 'Barangaroo', type: 'Café', lat: -33.8590, lng: 151.2007 },
    { name: 'Reserve Bowls', suburb: 'Barangaroo', type: 'Restaurant', lat: -33.8623, lng: 151.2026 },
    { name: 'Wynyard Provisions', suburb: 'Wynyard', type: 'Café', lat: -33.8654, lng: 151.2065 },
    { name: 'Carrington Coffee', suburb: 'Wynyard', type: 'Café', lat: -33.8649, lng: 151.2076 },
    { name: 'Martin Place Bowls', suburb: 'Sydney CBD', type: 'Restaurant', lat: -33.8676, lng: 151.2098 },
    { name: 'MLC Café', suburb: 'Sydney CBD', type: 'Café', lat: -33.8682, lng: 151.2095 },
    { name: 'Pitt St Roasters', suburb: 'Sydney CBD', type: 'Café', lat: -33.8730, lng: 151.2065 },
    { name: 'George St Greens', suburb: 'Sydney CBD', type: 'Restaurant', lat: -33.8722, lng: 151.2059 },
    { name: 'Park Lane Café', suburb: 'Hyde Park', type: 'Café', lat: -33.8736, lng: 151.2120 },
    { name: 'St James Bowls', suburb: 'Hyde Park', type: 'Restaurant', lat: -33.8703, lng: 151.2113 },
    { name: 'Liverpool St Kitchen', suburb: 'Sydney CBD', type: 'Restaurant', lat: -33.8770, lng: 151.2080 },
    { name: 'World Square Eats', suburb: 'Sydney CBD', type: 'Restaurant', lat: -33.8780, lng: 151.2070 },
    { name: 'Goulburn Provisions', suburb: 'Haymarket', type: 'Café', lat: -33.8800, lng: 151.2050 },
    { name: 'Chinatown Bowls', suburb: 'Haymarket', type: 'Restaurant', lat: -33.8795, lng: 151.2042 },
    { name: 'Darling Harbour Greens', suburb: 'Darling Harbour', type: 'Café', lat: -33.8742, lng: 151.1999 },
    { name: 'Tumbalong Café', suburb: 'Darling Harbour', type: 'Café', lat: -33.8780, lng: 151.1997 },
    { name: 'Harris St Coffee', suburb: 'Pyrmont', type: 'Café', lat: -33.8703, lng: 151.1956 },
    { name: 'Pyrmont Point Café', suburb: 'Pyrmont', type: 'Café', lat: -33.8675, lng: 151.1932 },
    { name: 'Star Bakery', suburb: 'Pyrmont', type: 'Bakery', lat: -33.8696, lng: 151.1944 },
    { name: 'Broadway Bowls', suburb: 'Ultimo', type: 'Restaurant', lat: -33.8800, lng: 151.1955 },
    { name: 'Quay St Greens', suburb: 'Ultimo', type: 'Café', lat: -33.8825, lng: 151.1990 },

    // ── Surry Hills / Darlinghurst / Potts Point / Kings Cross ──
    { name: 'Crown St Kitchen', suburb: 'Surry Hills', type: 'Restaurant', lat: -33.8853, lng: 151.2126 },
    { name: 'Bourke St Bakery', suburb: 'Surry Hills', type: 'Bakery', lat: -33.8880, lng: 151.2127 },
    { name: 'Riley St Coffee', suburb: 'Surry Hills', type: 'Café', lat: -33.8836, lng: 151.2113 },
    { name: 'Cleveland Greens', suburb: 'Surry Hills', type: 'Café', lat: -33.8895, lng: 151.2102 },
    { name: 'Devonshire Eats', suburb: 'Surry Hills', type: 'Restaurant', lat: -33.8865, lng: 151.2089 },
    { name: 'Foveaux St Roasters', suburb: 'Surry Hills', type: 'Café', lat: -33.8842, lng: 151.2075 },
    { name: 'Oxford Street Bowl Bar', suburb: 'Darlinghurst', type: 'Restaurant', lat: -33.8779, lng: 151.2191 },
    { name: 'Crown Lane Café', suburb: 'Darlinghurst', type: 'Café', lat: -33.8804, lng: 151.2186 },
    { name: 'Victoria St Provisions', suburb: 'Darlinghurst', type: 'Café', lat: -33.8769, lng: 151.2168 },
    { name: 'Macleay Greens', suburb: 'Potts Point', type: 'Café', lat: -33.8682, lng: 151.2237 },
    { name: 'Challis Ave Coffee', suburb: 'Potts Point', type: 'Café', lat: -33.8693, lng: 151.2247 },
    { name: 'Kings Cross Café', suburb: 'Kings Cross', type: 'Café', lat: -33.8744, lng: 151.2226 },
    { name: 'Bayswater Bowls', suburb: 'Kings Cross', type: 'Restaurant', lat: -33.8740, lng: 151.2243 },
    { name: 'Elizabeth Bay Greens', suburb: 'Elizabeth Bay', type: 'Café', lat: -33.8707, lng: 151.2278 },
    { name: 'Rushcutters Café', suburb: 'Rushcutters Bay', type: 'Café', lat: -33.8770, lng: 151.2294 },

    // ── Paddington / Woollahra / Centennial Park / Moore Park ──
    { name: 'Five Ways Café', suburb: 'Paddington', type: 'Café', lat: -33.8842, lng: 151.2298 },
    { name: 'Paddington Bowl Co.', suburb: 'Paddington', type: 'Restaurant', lat: -33.8864, lng: 151.2278 },
    { name: 'Glenmore Rd Roasters', suburb: 'Paddington', type: 'Café', lat: -33.8827, lng: 151.2317 },
    { name: 'Oxford Square Eats', suburb: 'Paddington', type: 'Restaurant', lat: -33.8848, lng: 151.2333 },
    { name: 'William St Bakery', suburb: 'Paddington', type: 'Bakery', lat: -33.8852, lng: 151.2349 },
    { name: 'Queen St Provisions', suburb: 'Woollahra', type: 'Bakery', lat: -33.8866, lng: 151.2387 },
    { name: 'Moncur Greens', suburb: 'Woollahra', type: 'Café', lat: -33.8902, lng: 151.2412 },
    { name: 'Centennial Park Café', suburb: 'Centennial Park', type: 'Café', lat: -33.8946, lng: 151.2336 },
    { name: 'Moore Park Bowls', suburb: 'Moore Park', type: 'Restaurant', lat: -33.8910, lng: 151.2235 },

    // ── Edgecliff / Double Bay / Bellevue Hill ──
    { name: 'Edgecliff Eats', suburb: 'Edgecliff', type: 'Restaurant', lat: -33.8786, lng: 151.2407 },
    { name: 'Edgecliff Station Coffee', suburb: 'Edgecliff', type: 'Café', lat: -33.8802, lng: 151.2410 },
    { name: 'Bay Street Roasters', suburb: 'Double Bay', type: 'Café', lat: -33.8780, lng: 151.2424 },
    { name: 'Cross St Café', suburb: 'Double Bay', type: 'Café', lat: -33.8772, lng: 151.2444 },
    { name: 'Knox St Bowls', suburb: 'Double Bay', type: 'Restaurant', lat: -33.8777, lng: 151.2417 },
    { name: 'Bellevue Hill Provisions', suburb: 'Bellevue Hill', type: 'Café', lat: -33.8835, lng: 151.2587 },
    { name: 'Birriga Bakery', suburb: 'Bellevue Hill', type: 'Bakery', lat: -33.8853, lng: 151.2615 },

    // ── Rose Bay / Vaucluse ──
    { name: 'Rose Bay Greens', suburb: 'Rose Bay', type: 'Café', lat: -33.8716, lng: 151.2683 },
    { name: 'New South Head Café', suburb: 'Rose Bay', type: 'Café', lat: -33.8734, lng: 151.2657 },
    { name: 'Lyne Park Eats', suburb: 'Rose Bay', type: 'Restaurant', lat: -33.8714, lng: 151.2706 },
    { name: 'Vaucluse Village Café', suburb: 'Vaucluse', type: 'Café', lat: -33.8589, lng: 151.2746 },
    { name: 'Hopetoun Bowls', suburb: 'Vaucluse', type: 'Restaurant', lat: -33.8600, lng: 151.2772 },

    // ── Bondi Junction / Bondi / Tamarama / Bronte ──
    { name: 'Junction Bowl Co.', suburb: 'Bondi Junction', type: 'Restaurant', lat: -33.8915, lng: 151.2484 },
    { name: 'Westfield Provisions', suburb: 'Bondi Junction', type: 'Café', lat: -33.8919, lng: 151.2517 },
    { name: 'Spring St Coffee', suburb: 'Bondi Junction', type: 'Café', lat: -33.8932, lng: 151.2497 },
    { name: 'Oxford St Bowls', suburb: 'Bondi Junction', type: 'Restaurant', lat: -33.8909, lng: 151.2532 },
    { name: 'Beachside Bowls', suburb: 'Bondi', type: 'Restaurant', lat: -33.8915, lng: 151.2767 },
    { name: 'Campbell Pde Café', suburb: 'Bondi', type: 'Café', lat: -33.8928, lng: 151.2769 },
    { name: 'Hall St Roasters', suburb: 'Bondi', type: 'Café', lat: -33.8909, lng: 151.2754 },
    { name: 'North Bondi Greens', suburb: 'North Bondi', type: 'Café', lat: -33.8889, lng: 151.2784 },
    { name: 'Notts Ave Bakery', suburb: 'Bondi', type: 'Bakery', lat: -33.8927, lng: 151.2755 },
    { name: 'Curlewis St Eats', suburb: 'Bondi', type: 'Restaurant', lat: -33.8922, lng: 151.2741 },
    { name: 'Tama Greens', suburb: 'Tamarama', type: 'Café', lat: -33.8997, lng: 151.2700 },
    { name: 'Tama Beach Café', suburb: 'Tamarama', type: 'Café', lat: -33.9006, lng: 151.2710 },
    { name: 'Bronte Beach House', suburb: 'Bronte', type: 'Café', lat: -33.9036, lng: 151.2654 },
    { name: 'Macpherson St Café', suburb: 'Bronte', type: 'Café', lat: -33.9054, lng: 151.2615 },

    // ── Clovelly / Coogee / Randwick / Kensington ──
    { name: 'Clovelly Kitchen', suburb: 'Clovelly', type: 'Restaurant', lat: -33.9148, lng: 151.2657 },
    { name: 'Clovelly Beach Café', suburb: 'Clovelly', type: 'Café', lat: -33.9155, lng: 151.2670 },
    { name: 'Coogee Coastal Café', suburb: 'Coogee', type: 'Café', lat: -33.9197, lng: 151.2589 },
    { name: 'Arden St Bowls', suburb: 'Coogee', type: 'Restaurant', lat: -33.9201, lng: 151.2575 },
    { name: 'Beach St Roasters', suburb: 'Coogee', type: 'Café', lat: -33.9207, lng: 151.2596 },
    { name: 'Brook St Bakery', suburb: 'Coogee', type: 'Bakery', lat: -33.9180, lng: 151.2598 },
    { name: 'High St Coffee', suburb: 'Randwick', type: 'Café', lat: -33.9173, lng: 151.2440 },
    { name: 'The Spot Café', suburb: 'Randwick', type: 'Café', lat: -33.9162, lng: 151.2460 },
    { name: 'Belmore Rd Bowls', suburb: 'Randwick', type: 'Restaurant', lat: -33.9148, lng: 151.2426 },
    { name: 'Coogee Bay Rd Eats', suburb: 'Randwick', type: 'Restaurant', lat: -33.9203, lng: 151.2470 },
    { name: 'Anzac Pde Eats', suburb: 'Kensington', type: 'Bakery', lat: -33.9089, lng: 151.2250 },
    { name: 'Day Ave Café', suburb: 'Kensington', type: 'Café', lat: -33.9103, lng: 151.2237 },
    { name: 'UNSW Bowls', suburb: 'Kensington', type: 'Restaurant', lat: -33.9170, lng: 151.2280 },
  ];

  React.useEffect(() => {
    if (!mapRef.current || mapInstanceRef.current) return;
    if (typeof L === 'undefined') return;

    const map = L.map(mapRef.current, {
      scrollWheelZoom: false,
      zoomControl: true,
    });

    // Fit the view tightly around the actual venue cluster — avoids hand-tuned
    // zoom that drifts as venues are added/removed, and adapts to screen size.
    const bounds = L.latLngBounds(venues.map(v => [v.lat, v.lng]));
    const fitOpts = { padding: [20, 20], maxZoom: 14 };
    map.fitBounds(bounds, fitOpts);

    L.tileLayer('https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png', {
      attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> &copy; <a href="https://carto.com/attributions">CARTO</a>',
      subdomains: 'abcd',
      maxZoom: 19,
    }).addTo(map);

    const markerIcon = L.divIcon({
      className: 'lup-marker',
      html: '<span></span>',
      iconSize: [16, 16],
      iconAnchor: [8, 8],
      popupAnchor: [0, -8],
    });

    venues.forEach(v => {
      L.marker([v.lat, v.lng], { icon: markerIcon })
        .addTo(map)
        .bindPopup(
          `<strong>${v.name}</strong><br/>` +
          `<span class="lup-popup-suburb">${v.suburb}</span> · ` +
          `<span class="lup-popup-type">${v.type}</span>`
        );
    });

    // Reveal animations / responsive layout can change container size after mount —
    // re-measure and re-fit once the page has settled so the bounds are accurate.
    setTimeout(() => {
      map.invalidateSize();
      map.fitBounds(bounds, fitOpts);
    }, 100);

    mapInstanceRef.current = map;
    return () => { map.remove(); mapInstanceRef.current = null; };
  }, []);

  return React.createElement('section', { id: 'locations', style: { background: 'var(--ivory)', borderTop: '1px solid var(--border-cream)' } },
    React.createElement('div', { className: 'container' },
      // Header
      React.createElement(Reveal, { as: 'div',
        style: { maxWidth: 760, marginBottom: 32 }
      },
        React.createElement('div', { className: 'eyebrow', style: { marginBottom: 18 } },
          React.createElement('span', { className: 'dot' }), 'LÜP locations'
        ),
        React.createElement('h2', {
          style: { fontSize: 'clamp(36px, 4vw, 56px)', lineHeight: 1.05, letterSpacing: '-0.02em', marginBottom: 16 }
        },
          header.leadText,
          React.createElement('span', { className: 'serif-accent' }, header.accentText)
        ),
        React.createElement('p', { style: { fontSize: 17, color: 'var(--stone)', lineHeight: 1.6 } },
          header.subText
        ),
      ),

      // Map container
      React.createElement('div', {
        ref: mapRef,
        className: 'lup-map',
        style: {
          height: 'clamp(420px, 60vh, 560px)',
          borderRadius: 24,
          overflow: 'hidden',
          border: '1px solid var(--border-cream)',
          boxShadow: '0 4px 30px rgba(0,0,0,0.04)',
          background: 'var(--parchment)',
        }
      }),
    ),
  );
};
