// PitStop UI Kit — in-app notification centre (#395)
// Recreated from apps/web/src/components/NotificationBell.tsx. One inbox across
// all trips, opened from the bell in the Home and trip headers.

const SAMPLE_NOTIFICATIONS = [
  { id: 'n1', icon: 'message-circle', title: 'New comment on your highlight', body: '“That descent was unreal”', trip: 'Dolomites loop', when: '4m', unread: true },
  { id: 'n2', icon: 'coins', title: 'New expense', body: 'Rifugio dinner', trip: 'Dolomites loop', when: '1h', unread: true },
  { id: 'n3', icon: 'map-pin', title: 'New stop on the trip', body: 'Passo Giau', trip: 'Dolomites loop', when: '3h', unread: false },
  { id: 'n4', icon: 'play-circle', title: 'Yesterday’s recap is ready', body: 'Relive yesterday on Peak District', trip: 'Peak District', when: '2d', unread: false },
];

const headerButton = {
  position: 'relative', width: 40, height: 40, borderRadius: 999, border: '1px solid hsl(var(--line))',
  background: 'transparent', display: 'flex', alignItems: 'center', justifyContent: 'center',
  cursor: 'pointer', color: 'hsl(var(--ink))', flexShrink: 0,
};

/** Bell with an unread badge: exact up to 9, then "9+". */
function NotificationBell({ count = 0, onOpen }) {
  const badge = count <= 0 ? null : count > 9 ? '9+' : String(count);
  return (
    <button onClick={onOpen} aria-label={badge ? `Notifications, ${count} unread` : 'Notifications'} style={headerButton}>
      <Icon name="bell" size={20} />
      {badge && (
        <span style={{ position: 'absolute', top: -4, right: -4, minWidth: 18, height: 18, padding: '0 4px', borderRadius: 999,
          background: 'hsl(var(--ember))', color: 'hsl(var(--paper))', fontFamily: 'var(--font-sans)', fontWeight: 900,
          fontSize: 11, lineHeight: '18px', textAlign: 'center' }}>{badge}</span>
      )}
    </button>
  );
}

/** Profile button: the signed-in user's initials. */
function ProfileButton({ initials }) {
  return (
    <div style={{ ...headerButton, fontFamily: 'var(--font-sans)', fontWeight: 800, fontSize: 13 }}>{initials}</div>
  );
}

function NotificationRow({ n, onOpen }) {
  return (
    <button onClick={() => onOpen(n)} style={{ width: '100%', textAlign: 'left', display: 'flex', alignItems: 'flex-start', gap: 12,
      padding: 12, borderRadius: 12, cursor: 'pointer',
      background: n.unread ? 'hsl(var(--paper))' : 'hsl(var(--bg))',
      border: `1px solid ${n.unread ? 'hsl(var(--line))' : 'transparent'}` }}>
      <span style={{ width: 36, height: 36, borderRadius: 999, background: 'var(--ember-soft)', color: 'hsl(var(--ember))',
        display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0 }}>
        <Icon name={n.icon} size={16} />
      </span>
      <span style={{ flex: 1, minWidth: 0 }}>
        <span style={{ display: 'flex', alignItems: 'baseline', gap: 8 }}>
          <span style={{ flex: 1, minWidth: 0, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap',
            fontFamily: 'var(--font-sans)', fontSize: 14, fontWeight: n.unread ? 700 : 500, color: 'hsl(var(--ink))' }}>{n.title}</span>
          <span style={{ flexShrink: 0, fontSize: 12, color: 'hsl(var(--muted-ink))' }}>{n.when}</span>
        </span>
        <span style={{ display: 'block', fontSize: 14, color: 'hsl(var(--muted-ink))', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{n.body}</span>
        <span style={{ display: 'block', marginTop: 4, fontSize: 11, color: 'hsl(var(--muted-ink))' }}>{n.trip}</span>
      </span>
      {n.unread && <span aria-label="Unread" style={{ marginTop: 6, width: 8, height: 8, borderRadius: 999, background: 'hsl(var(--ember))', flexShrink: 0 }} />}
    </button>
  );
}

/** Bottom sheet listing the latest 20 notifications; tap marks read and deep-links. */
function NotificationSheet({ items, onClose, onOpen, onMarkAll }) {
  useLucide();
  const unread = items.filter(n => n.unread).length;
  return (
    <div onClick={onClose} style={{ position: 'absolute', inset: 0, zIndex: 40, background: 'hsl(var(--ink) / 0.35)', display: 'flex', alignItems: 'flex-end' }}>
      <div onClick={e => e.stopPropagation()} style={{ width: '100%', maxHeight: '80%', overflowY: 'auto', background: 'hsl(var(--bg))',
        borderRadius: '20px 20px 0 0', padding: '16px 16px 40px' }}>
        <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 8 }}>
          <span style={{ fontFamily: 'var(--font-sans)', fontWeight: 900, fontSize: 18, color: 'hsl(var(--ink))' }}>Notifications</span>
          <button onClick={onClose} aria-label="Close" style={{ ...headerButton, border: 'none', width: 36, height: 36 }}><Icon name="x" size={20} /></button>
        </div>
        {unread > 0 && (
          <div style={{ display: 'flex', justifyContent: 'flex-end', marginBottom: 8 }}>
            <button onClick={onMarkAll} style={{ border: 'none', background: 'transparent', fontWeight: 700, fontSize: 13, color: 'hsl(var(--ink))', cursor: 'pointer' }}>Mark all as read</button>
          </div>
        )}
        {items.length === 0 ? (
          <div style={{ padding: 16, textAlign: 'center' }}>
            <div style={{ fontWeight: 700, color: 'hsl(var(--ink))' }}>You're all caught up</div>
            <div style={{ fontSize: 14, color: 'hsl(var(--muted-ink))', marginTop: 4 }}>New expenses, stops, highlights and mentions on your trips show up here.</div>
          </div>
        ) : (
          <div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
            {items.map(n => <NotificationRow key={n.id} n={n} onOpen={onOpen} />)}
          </div>
        )}
      </div>
    </div>
  );
}

/** Kit-local inbox state shared by the Home and trip headers. */
function useNotificationInbox() {
  const [items, setItems] = useState(SAMPLE_NOTIFICATIONS);
  const [open, setOpen] = useState(false);
  const read = (id) => setItems(prev => prev.map(n => (id === undefined || n.id === id) ? { ...n, unread: false } : n));
  return {
    unread: items.filter(n => n.unread).length,
    openSheet: () => setOpen(true),
    sheet: open && <NotificationSheet items={items} onClose={() => setOpen(false)}
      onOpen={(n) => { read(n.id); setOpen(false); }} onMarkAll={() => read()} />,
  };
}

Object.assign(window, { NotificationBell, ProfileButton, NotificationSheet, useNotificationInbox, SAMPLE_NOTIFICATIONS });
