// Shared atoms: phone frame, status bar, icons, niches data, lucide-style svg icons.
// Globals exposed to window for cross-file use.

const { useState, useEffect, useRef, useMemo } = React;

/* ============== ICONS (inline SVG, lucide-style stroke 2) ============== */
const Icon = ({ d, size = 20, stroke = 'currentColor', fill = 'none', sw = 1.8, children, style }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill={fill} stroke={stroke} strokeWidth={sw}
       strokeLinecap="round" strokeLinejoin="round" style={style}>
    {d ? <path d={d} /> : children}
  </svg>
);
const IconCamera = (p) => <Icon {...p}><path d="M3 7h3l2-3h8l2 3h3v13H3z"/><circle cx="12" cy="13" r="4"/></Icon>;
const IconBolt   = (p) => <Icon {...p} d="M13 2 3 14h7l-1 8 11-12h-7z"/>;
const IconPin    = (p) => <Icon {...p}><path d="M12 22s-7-7-7-12a7 7 0 1 1 14 0c0 5-7 12-7 12z"/><circle cx="12" cy="10" r="2.5"/></Icon>;
const IconChat   = (p) => <Icon {...p}><path d="M21 12a8 8 0 0 1-11.6 7.1L4 21l1.9-5.4A8 8 0 1 1 21 12z"/></Icon>;
const IconStar   = (p) => <Icon {...p} d="m12 2 3 7 7 .8-5.4 4.8L18 22l-6-3.7L6 22l1.4-7.4L2 9.8 9 9z"/>;
const IconCheck  = (p) => <Icon {...p} d="m5 12 5 5 9-12"/>;
const IconArrow  = (p) => <Icon {...p} d="M5 12h14m-6-6 6 6-6 6"/>;
const IconShield = (p) => <Icon {...p} d="M12 2 4 5v7c0 5 3.5 8.5 8 10 4.5-1.5 8-5 8-10V5z"/>;
const IconClock  = (p) => <Icon {...p}><circle cx="12" cy="12" r="9"/><path d="M12 7v5l3 2"/></Icon>;
const IconReceipt= (p) => <Icon {...p}><path d="M5 3v18l3-2 2 2 2-2 2 2 2-2 3 2V3z"/><path d="M8 8h8M8 12h6M8 16h4"/></Icon>;
const IconPlus   = (p) => <Icon {...p} d="M12 5v14M5 12h14"/>;
const IconQuestion = (p) => <Icon {...p}><circle cx="12" cy="12" r="9"/><path d="M9.5 9a2.5 2.5 0 1 1 4 2c-1 .8-1.5 1.5-1.5 3"/><circle cx="12" cy="17" r=".5"/></Icon>;
const IconHome   = (p) => <Icon {...p} d="M3 11 12 3l9 8v10h-6v-6h-6v6H3z"/>;
const IconUser   = (p) => <Icon {...p}><circle cx="12" cy="8" r="4"/><path d="M4 21c1-4 4-6 8-6s7 2 8 6"/></Icon>;
const IconHeart  = (p) => <Icon {...p} d="M20.8 4.6a5.5 5.5 0 0 0-7.8 0L12 5.6 11 4.6a5.5 5.5 0 1 0-7.8 7.8l8.8 8.8 8.8-8.8a5.5 5.5 0 0 0 0-7.8z"/>;

/* Niche emoji-icons replaced with inline glyphs (no emoji per design system) */
const NicheGlyphs = {
  plumbing:  (s=22, c='currentColor') => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.8" strokeLinecap="round"><path d="M5 4v5a3 3 0 0 0 3 3h4a3 3 0 0 1 3 3v5"/><path d="M3 4h4M13 17h4M19 7l2-2-3-3-2 2z"/></svg>,
  hvac:      (s=22, c='currentColor') => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.8" strokeLinecap="round"><circle cx="12" cy="12" r="3"/><path d="M12 3v6M12 15v6M3 12h6M15 12h6M5 5l4 4M15 15l4 4M5 19l4-4M15 9l4-4"/></svg>,
  electrical:(s=22, c='currentColor') => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.8" strokeLinejoin="round" strokeLinecap="round"><path d="M13 2 4 14h7l-1 8 10-12h-7z"/></svg>,
  roofing:   (s=22, c='currentColor') => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M2 12 12 3l10 9M5 11v9h14v-9"/></svg>,
  painting:  (s=22, c='currentColor') => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M4 5h13v5H4zM10 10v4a2 2 0 0 0 2 2h2v4h-3v-4"/></svg>,
  cleaning:  (s=22, c='currentColor') => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="m14 4 6 6M3 21l8-8M9 11l4 4M15 5l4 4-7 7H8v-4z"/></svg>,
  lawn:      (s=22, c='currentColor') => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M3 20h18M5 20v-3a3 3 0 0 1 3-3h2v6M14 20v-8M14 12c0-3 2-5 5-5"/></svg>,
  pest:      (s=22, c='currentColor') => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><ellipse cx="12" cy="14" rx="5" ry="6"/><path d="M9 9 7 6M15 9l2-3M7 14H4M17 14h3M8 19l-2 2M16 19l2 2"/></svg>,
  handyman:  (s=22, c='currentColor') => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="m14 6 4 4-9 9-4-4z"/><path d="M14 6 17 3l4 4-3 3M7 17l-3 3"/></svg>,
  appliance: (s=22, c='currentColor') => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.8" strokeLinecap="round"><rect x="5" y="3" width="14" height="18" rx="2"/><path d="M5 9h14"/><circle cx="12" cy="15" r="3"/></svg>,
  garage:    (s=22, c='currentColor') => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M3 11 12 4l9 7v9H3zM7 14h10M7 17h10"/></svg>,
  locksmith: (s=22, c='currentColor') => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.8" strokeLinecap="round"><circle cx="9" cy="13" r="4"/><path d="m12 12 9-2-1 3 2 1-2 2"/></svg>,
};

/* ============== NICHE DATA ============== */
const NICHES = [
  { id: 'plumbing',   name: 'Plumbing',     ex: 'Leaky faucet',         price: '$165 – $340',  avg: '~$240' },
  { id: 'hvac',       name: 'HVAC',         ex: 'AC not cooling',       price: '$220 – $680',  avg: '~$420' },
  { id: 'electrical', name: 'Electrical',   ex: 'Outlet not working',   price: '$140 – $380',  avg: '~$210' },
  { id: 'roofing',    name: 'Roofing',      ex: 'Missing shingles',     price: '$380 – $1.2k', avg: '~$640' },
  { id: 'painting',   name: 'Painting',     ex: 'Repaint living room',  price: '$420 – $1.8k', avg: '~$880' },
  { id: 'cleaning',   name: 'Cleaning',     ex: 'Deep clean 3BR',       price: '$180 – $360',  avg: '~$245' },
  { id: 'lawn',       name: 'Lawn & Yard',  ex: 'Mow + edge',           price: '$60 – $180',   avg: '~$95'  },
  { id: 'pest',       name: 'Pest Control', ex: 'Ant treatment',        price: '$120 – $280',  avg: '~$170' },
  { id: 'handyman',   name: 'Handyman',     ex: 'Mount 4 shelves',      price: '$110 – $260',  avg: '~$160' },
  { id: 'appliance',  name: 'Appliance',    ex: 'Dishwasher repair',    price: '$180 – $420',  avg: '~$260' },
  { id: 'garage',     name: 'Garage Door',  ex: 'Spring replacement',   price: '$240 – $480',  avg: '~$340' },
  { id: 'locksmith',  name: 'Locksmith',    ex: 'Rekey 3 locks',        price: '$90 – $210',   avg: '~$135' },
];

/* ============== PHONE FRAME ============== */
const Phone = ({ width = 320, height = 660, statusDark = false, time = '9:41', children, style, screenStyle, notch = true }) => (
  <div className="phone" style={{ width, height, ...style }}>
    <div className="phone-screen" style={screenStyle}>
      {notch && <div className="phone-notch" />}
      <div className={'status-bar' + (statusDark ? ' dark' : '')}>
        <span>{time}</span>
        <div className="status-bar-right">
          <SvgSignal dark={statusDark} />
          <SvgWifi dark={statusDark} />
          <SvgBattery dark={statusDark} />
        </div>
      </div>
      {children}
    </div>
  </div>
);

const SvgSignal = ({ dark }) => (
  <svg width="18" height="11" viewBox="0 0 18 11" fill={dark ? '#fff' : '#0c0e10'}>
    <rect x="0" y="7" width="3" height="4" rx=".6"/>
    <rect x="5" y="5" width="3" height="6" rx=".6"/>
    <rect x="10" y="2" width="3" height="9" rx=".6"/>
    <rect x="15" y="0" width="3" height="11" rx=".6"/>
  </svg>
);
const SvgWifi = ({ dark }) => (
  <svg width="17" height="12" viewBox="0 0 17 12" fill="none" stroke={dark ? '#fff' : '#0c0e10'} strokeWidth="1.6" strokeLinecap="round">
    <path d="M1 4.5C3 2.7 5.5 1.5 8.5 1.5s5.5 1.2 7.5 3"/>
    <path d="M3.5 7.5c1.4-1.2 3-2 5-2s3.6.8 5 2"/>
    <circle cx="8.5" cy="10.5" r=".9" fill={dark ? '#fff' : '#0c0e10'} stroke="none"/>
  </svg>
);
const SvgBattery = ({ dark }) => (
  <svg width="27" height="13" viewBox="0 0 27 13" fill="none">
    <rect x="0.5" y="0.5" width="22" height="12" rx="3" stroke={dark ? '#fff' : '#0c0e10'} strokeOpacity=".5"/>
    <rect x="2" y="2" width="19" height="9" rx="1.6" fill={dark ? '#fff' : '#0c0e10'}/>
    <rect x="23" y="4" width="2" height="5" rx=".8" fill={dark ? '#fff' : '#0c0e10'} fillOpacity=".5"/>
  </svg>
);

/* ============== APP STORE BADGES ============== */
const AppStoreBadge = ({ store = 'app' }) => (
  <a href="#" className="appstore-badge">
    {store === 'app' ? (
      <svg width="22" height="26" viewBox="0 0 22 26" fill="currentColor"><path d="M15.7 6.7c-1.3 0-2.7-.8-3.6-.8-1 0-2.7.8-3.8.8-2.3 0-4.3-2-4.3-5C4 -.6 6.8-1 8 1c.5.8 1.3 1.4 2.4 1.4 1.1 0 1.8-.6 2.4-1.4 1.2-2 4-1.6 4.5 1.7-1.6 1-2.1 3-2.1 4.6 0 1.6 1 3.3 2.2 4-.6 1.8-1.5 3.6-2.7 3.6z" transform="translate(0 4)"/><path d="M11 5.5c-.2-1.4 1-3 2.2-3.5C13.3 3.7 12 5.4 11 5.5z"/></svg>
    ) : (
      <svg width="22" height="24" viewBox="0 0 22 24" fill="currentColor"><path d="M2 1.5 12 12 2 22.5z" opacity=".9"/><path d="m13.5 10.5 3 1.5-3 1.5L12 12z" opacity=".7"/><path d="M2 1.5 13.5 10.5 12 12z" opacity=".6"/><path d="M2 22.5 13.5 13.5 12 12z" opacity=".5"/></svg>
    )}
    <div>
      <div className="_label">{store === 'app' ? 'Download on the' : 'Get it on'}</div>
      <div className="_store">{store === 'app' ? 'App Store' : 'Google Play'}</div>
    </div>
  </a>
);

/* ============== AVATARS (placeholder colorful discs) ============== */
const Avatar = ({ name = 'JM', size = 32, hue = 220 }) => (
  <div style={{
    width: size, height: size, borderRadius: '50%',
    background: `linear-gradient(135deg, hsl(${hue}, 70%, 70%), hsl(${(hue + 40) % 360}, 70%, 55%))`,
    color: '#fff', display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
    fontFamily: 'var(--font-display)', fontWeight: 600, fontSize: size * 0.36, letterSpacing: '-0.02em',
    boxShadow: '0 1px 0 rgba(255,255,255,.4) inset',
    flexShrink: 0,
  }}>{name}</div>
);

/* ============== HOMEPRO LOGO ============== */
// The HP monogram in a chartreuse→lilac ring, with a smiling house in the negative space.
// Renders the PNG with adjustable size + optional dark background tile.
const Logo = ({ size = 32, tile = false, tileColor = 'var(--ink)' }) => {
  const inner = (
    <img src="../assets/logo.jpg" alt="homepro"
      style={{ width: size, height: size, display:'block', objectFit:'contain' }}
    />
  );
  if (tile) {
    return (
      <div style={{ width: size + 6, height: size + 6, borderRadius: (size+6) * 0.28, background: tileColor, display:'flex', alignItems:'center', justifyContent:'center', flexShrink:0 }}>
        {inner}
      </div>
    );
  }
  return <span style={{ display:'inline-flex', flexShrink:0 }}>{inner}</span>;
};

const Wordmark = ({ size = 18, color = 'var(--ink)' }) => (
  <span style={{ fontFamily:'var(--font-display)', fontWeight:700, fontSize:size, letterSpacing:'-0.02em', color, lineHeight:1 }}>
    home<span style={{ color:'var(--lilac-600)' }}>pro</span>
  </span>
);

/* Expose to window */
Object.assign(window, {
  // React hooks (needed by service-picker.jsx which uses bare names)
  useState, useEffect, useRef, useMemo,
  // Icons
  Icon, IconCamera, IconBolt, IconPin, IconChat, IconStar, IconCheck, IconArrow, IconShield,
  IconClock, IconReceipt, IconPlus, IconQuestion, IconHome, IconUser, IconHeart,
  // Components
  NicheGlyphs, NICHES, Phone, AppStoreBadge, Avatar, Logo, Wordmark,
});
