// Shared primitives for PlaceSpace flyers
// - PSLogo: the emerald app mark (recreated from the logo PNG shape so it
//   stays crisp at poster scale).
// - QRCode: fake but plausible-looking QR block. Real print would need a
//   generated QR pointing to placespace.co or a space-specific deep link.
// - AppStoreBadges: App Store + Google Play pills (line art — no official
//   artwork pulled in).
// - StorePills, etc.

const BRAND = {
  emerald50:  '#ecfdf5',
  emerald100: '#d1fae5',
  emerald200: '#a7f3d0',
  emerald300: '#6ee7b7',
  emerald400: '#34d399',
  emerald500: '#10b981',
  emerald600: '#059669',
  emerald700: '#047857',
  emerald800: '#065f46',
  emerald900: '#064e3b',
  teal500:    '#14b8a6',
  gray50:  '#f9fafb',
  gray100: '#f3f4f6',
  gray200: '#e5e7eb',
  gray300: '#d1d5db',
  gray400: '#9ca3af',
  gray500: '#6b7280',
  gray600: '#4b5563',
  gray700: '#374151',
  gray800: '#1f2937',
  gray900: '#111827',
  white: '#ffffff',
  black: '#000000',
  font: 'ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif',
};

// PlaceSpace mark — the actual logo PNG.
// The `color` prop is retained for API compatibility but is ignored; the
// logo is rendered as-is from assets/logo.png. `inverted` swaps to a white
// tinted version via CSS filter (used on dark backgrounds).
function PSLogo({ size = 96, color, inverted = false, shadow = false }) {
  // Detect "white on dark" use-cases: callers pass BRAND.white as `color`
  // when the surrounding surface is dark. In those cases, tint the logo to
  // white so it reads against the background.
  const tintWhite = color === BRAND.white || inverted;
  return (
    <img
      src="/landings/assets/logo.png"
      width={size}
      height={size}
      alt="PlaceSpace"
      style={{
        display: 'block',
        width: size, height: size,
        filter: [
          shadow ? 'drop-shadow(0 8px 24px rgba(0,0,0,.25))' : '',
          tintWhite ? 'brightness(0) invert(1)' : '',
        ].filter(Boolean).join(' ') || 'none',
      }}
    />
  );
}

// Pseudo QR — 25×25 matrix that looks right from any distance. For real
// production the user should swap in a generated QR (e.g. pointing to
// https://placespace.co or a deep link). Three finder squares at the
// corners are authentic to QR; the rest is decorative noise with stable
// pseudo-random dots so it renders identically on every load.
function QRCode({ size = 220, fg = BRAND.gray900, bg = BRAND.white, radius = 0, quietZone = 14 }) {
  const cells = 25;
  const inner = size - quietZone * 2;
  const cell = inner / cells;
  // Stable pseudo-random pattern
  const rand = (i, j) => {
    const x = Math.sin(i * 374.2 + j * 611.7) * 43758.5453;
    return x - Math.floor(x);
  };
  const isFinder = (i, j) => (
    (i < 7 && j < 7) ||          // top-left
    (i < 7 && j > cells - 8) ||  // top-right
    (i > cells - 8 && j < 7)     // bottom-left
  );
  const Finder = ({ x, y }) => (
    <g transform={`translate(${x},${y})`}>
      <rect x="0" y="0" width={cell * 7} height={cell * 7} fill={fg} />
      <rect x={cell} y={cell} width={cell * 5} height={cell * 5} fill={bg} />
      <rect x={cell * 2} y={cell * 2} width={cell * 3} height={cell * 3} fill={fg} />
    </g>
  );
  const dots = [];
  for (let i = 0; i < cells; i++) {
    for (let j = 0; j < cells; j++) {
      if (isFinder(i, j)) continue;
      if (rand(i, j) > 0.55) {
        dots.push(<rect key={`${i}-${j}`} x={quietZone + i * cell} y={quietZone + j * cell} width={cell * 0.95} height={cell * 0.95} fill={fg} />);
      }
    }
  }
  return (
    <svg width={size} height={size} viewBox={`0 0 ${size} ${size}`} style={{ display: 'block', background: bg, borderRadius: radius }}>
      <rect x="0" y="0" width={size} height={size} fill={bg} rx={radius} />
      {dots}
      <g transform={`translate(${quietZone},${quietZone})`}>
        <Finder x={0} y={0} />
        <Finder x={cell * (cells - 7)} y={0} />
        <Finder x={0} y={cell * (cells - 7)} />
      </g>
    </svg>
  );
}

// App Store / Google Play badges — line-art recreations. Real printed
// flyers should use the official brand artwork from each store's asset
// kit. These are placeholders that read correctly at a distance.
function AppStoreBadge({ color = BRAND.white, bg = BRAND.black, width = 158 }) {
  const height = width * (54 / 158);
  const padX = width * 0.07;
  const gap = width * 0.05;
  return (
    <a href="https://apps.apple.com/us/app/placespace-app/id6754170618" target="_blank" rel="noopener noreferrer" style={{ textDecoration: 'none', color: 'inherit', display: 'inline-block' }}>
    <div style={{
      width, height, background: bg, color,
      borderRadius: width * 0.05, display: 'flex', alignItems: 'center',
      gap, padding: `0 ${padX}px`, fontFamily: BRAND.font, boxSizing: 'border-box',
      overflow: 'hidden', cursor: 'pointer',
    }}>
      <svg width={width * 0.17} height={width * 0.2} viewBox="0 0 24 28" fill={color} style={{ flexShrink: 0 }}>
        <path d="M17.05 14.46c-.03-2.96 2.42-4.38 2.53-4.45-1.38-2.02-3.52-2.3-4.28-2.33-1.81-.19-3.55 1.08-4.47 1.08-.94 0-2.35-1.06-3.87-1.03-1.99.03-3.83 1.16-4.86 2.94-2.1 3.63-.53 9 1.48 11.95.99 1.45 2.15 3.07 3.67 3.01 1.48-.06 2.04-.95 3.83-.95 1.77 0 2.29.95 3.85.92 1.59-.03 2.6-1.47 3.57-2.93 1.14-1.67 1.6-3.32 1.62-3.4-.04-.02-3.11-1.19-3.14-4.71zm-2.94-8.67c.82-.99 1.37-2.38 1.22-3.76-1.18.05-2.6.79-3.45 1.78-.76.88-1.43 2.28-1.25 3.64 1.31.1 2.66-.67 3.48-1.66z"/>
      </svg>
      <div style={{ lineHeight: 1.1, minWidth: 0, flex: 1 }}>
        <div style={{ fontSize: width * 0.065, opacity: 0.8, fontWeight: 400, marginBottom: 1, whiteSpace: 'nowrap', letterSpacing: 0.2 }}>Download on the</div>
        <div style={{ fontSize: width * 0.115, fontWeight: 600, letterSpacing: -0.2, whiteSpace: 'nowrap' }}>App&nbsp;Store</div>
      </div>
    </div>
    </a>
  );
}

function GooglePlayBadge({ color = BRAND.white, bg = BRAND.black, width = 158 }) {
  const height = width * (54 / 158);
  const padX = width * 0.07;
  const gap = width * 0.05;
  return (
    <a href="https://play.google.com/store/apps/details?id=com.placespace.app" target="_blank" rel="noopener noreferrer" style={{ textDecoration: 'none', color: 'inherit', display: 'inline-block' }}>
    <div style={{
      width, height, background: bg, color,
      borderRadius: width * 0.05, display: 'flex', alignItems: 'center',
      gap, padding: `0 ${padX}px`, fontFamily: BRAND.font, boxSizing: 'border-box',
      overflow: 'hidden', cursor: 'pointer',
    }}>
      <svg width={width * 0.17} height={width * 0.2} viewBox="0 0 28 32" style={{ flexShrink: 0 }}>
        <path d="M3.6 1.2c-.5.4-.8 1-.8 1.9v25.8c0 .8.3 1.5.8 1.9l15.2-14.8L3.6 1.2z" fill="#34d399"/>
        <path d="M23.3 12.3 19.3 10l-3.5 3.4 3.5 3.4 4-2.3c1.4-.8 1.4-2.4 0-3.2z" fill="#fbbc04"/>
        <path d="M18.8 15.4 3.6 30.8c.5.4 1.4.5 2.3 0l17.4-10-4.5-5.4z" fill="#ea4335"/>
        <path d="M18.8 15.4 5.9 1.2c-.9-.5-1.8-.4-2.3 0l15.2 14.2z" fill="#4285f4"/>
      </svg>
      <div style={{ lineHeight: 1.1, minWidth: 0, flex: 1 }}>
        <div style={{ fontSize: width * 0.065, opacity: 0.8, fontWeight: 400, marginBottom: 1, whiteSpace: 'nowrap', letterSpacing: 0.2 }}>GET IT ON</div>
        <div style={{ fontSize: width * 0.115, fontWeight: 600, letterSpacing: -0.2, whiteSpace: 'nowrap' }}>Google&nbsp;Play</div>
      </div>
    </div>
    </a>
  );
}

// Generated dummy avatars — consistent across flyers. Uses initials on a
// colored circle so we don't fake real photos. Real flyers would swap in
// actual member avatars or press imagery.
// Names paired with a pravatar img id so the same person renders the same
// photo across every flyer. pravatar.cc serves MIT-licensed headshots and
// is fine for mockups — real flyers should swap in actual member avatars.
const AVATAR_NAMES = [
  { name: 'Marcus', hue: 215, img: 12 },
  { name: 'Sarah',  hue: 345, img: 5  },
  { name: 'Priya',  hue: 30,  img: 47 },
  { name: 'James',  hue: 165, img: 33 },
  { name: 'Leila',  hue: 275, img: 49 },
  { name: 'Ben',    hue: 195, img: 14 },
  { name: 'Ava',    hue: 10,  img: 25 },
  { name: 'Noah',   hue: 140, img: 52 },
  { name: 'Zoe',    hue: 305, img: 44 },
  { name: 'Ren',    hue: 75,  img: 60 },
];

function Avatar({ name, hue, img, size = 40, ring = null, photo = true }) {
  const initial = name[0];
  const base = {
    width: size, height: size, borderRadius: '50%',
    flexShrink: 0,
    boxShadow: ring ? `0 0 0 ${Math.max(2, size * 0.06)}px ${ring}` : 'none',
  };
  if (photo && img != null) {
    return (
      <div style={{
        ...base,
        background: `linear-gradient(135deg, hsl(${hue} 70% 55%), hsl(${hue} 80% 40%))`,
        backgroundImage: `url(https://i.pravatar.cc/300?img=${img})`,
        backgroundSize: 'cover', backgroundPosition: 'center',
      }}/>
    );
  }
  return (
    <div style={{
      ...base,
      background: `linear-gradient(135deg, hsl(${hue} 70% 55%), hsl(${hue} 80% 40%))`,
      color: BRAND.white,
      fontFamily: BRAND.font, fontWeight: 600,
      fontSize: size * 0.42,
      display: 'flex', alignItems: 'center', justifyContent: 'center',
    }}>{initial}</div>
  );
}

// Lucide icons embedded inline (no script-load dependency — these are the
// exact stroke paths Lucide ships). Minimal set used across flyers.
const IconAnchor = ({ size = 24, color = 'currentColor', strokeWidth = 1.8 }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth={strokeWidth} strokeLinecap="round" strokeLinejoin="round">
    <circle cx="12" cy="5" r="3"/><line x1="12" y1="22" x2="12" y2="8"/><path d="M5 12H2a10 10 0 0 0 20 0h-3"/>
  </svg>
);
const IconMapPin = ({ size = 24, color = 'currentColor', strokeWidth = 1.8 }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth={strokeWidth} strokeLinecap="round" strokeLinejoin="round">
    <path d="M20 10c0 7-8 12-8 12s-8-5-8-12a8 8 0 0 1 16 0Z"/><circle cx="12" cy="10" r="3"/>
  </svg>
);
const IconUsers = ({ size = 24, color = 'currentColor', strokeWidth = 1.8 }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth={strokeWidth} strokeLinecap="round" strokeLinejoin="round">
    <path d="M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M22 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/>
  </svg>
);
const IconCalendar = ({ size = 24, color = 'currentColor', strokeWidth = 1.8 }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth={strokeWidth} strokeLinecap="round" strokeLinejoin="round">
    <rect x="3" y="4" width="18" height="18" rx="2"/><line x1="16" y1="2" x2="16" y2="6"/><line x1="8" y1="2" x2="8" y2="6"/><line x1="3" y1="10" x2="21" y2="10"/>
  </svg>
);
const IconArrowRight = ({ size = 24, color = 'currentColor', strokeWidth = 2 }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth={strokeWidth} strokeLinecap="round" strokeLinejoin="round">
    <line x1="5" y1="12" x2="19" y2="12"/><polyline points="12 5 19 12 12 19"/>
  </svg>
);
const IconSparkles = ({ size = 24, color = 'currentColor', strokeWidth = 1.8 }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth={strokeWidth} strokeLinecap="round" strokeLinejoin="round">
    <path d="m12 3-1.9 5.8a2 2 0 0 1-1.3 1.3L3 12l5.8 1.9a2 2 0 0 1 1.3 1.3L12 21l1.9-5.8a2 2 0 0 1 1.3-1.3L21 12l-5.8-1.9a2 2 0 0 1-1.3-1.3L12 3Z"/>
  </svg>
);
const IconMessageCircle = ({ size = 24, color = 'currentColor', strokeWidth = 1.8 }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth={strokeWidth} strokeLinecap="round" strokeLinejoin="round">
    <path d="M7.9 20A9 9 0 1 0 4 16.1L2 22Z"/>
  </svg>
);
const IconHeart = ({ size = 24, color = 'currentColor', strokeWidth = 1.8 }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth={strokeWidth} strokeLinecap="round" strokeLinejoin="round">
    <path d="M20.84 4.61a5.5 5.5 0 0 0-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 0 0-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 0 0 0-7.78z"/>
  </svg>
);

Object.assign(window, {
  BRAND, PSLogo, QRCode, AppStoreBadge, GooglePlayBadge,
  Avatar, AVATAR_NAMES,
  IconAnchor, IconMapPin, IconUsers, IconCalendar, IconArrowRight, IconSparkles, IconMessageCircle, IconHeart,
});
