// Listing detail modal with photo gallery

const ListingModal = ({ listing, onClose, setRoute }) => {
  const [idx, setIdx] = React.useState(0);
  useLucide();
  React.useEffect(() => {
    const onKey = (e) => {
      if (e.key === 'Escape') onClose();
      if (e.key === 'ArrowLeft') setIdx(i => (i - 1 + listing.images.length) % listing.images.length);
      if (e.key === 'ArrowRight') setIdx(i => (i + 1) % listing.images.length);
    };
    window.addEventListener('keydown', onKey);
    document.body.style.overflow = 'hidden';
    return () => {
      window.removeEventListener('keydown', onKey);
      document.body.style.overflow = '';
    };
  }, [listing]);

  if (!listing) return null;

  return (
    <div className="modal-backdrop" onClick={onClose}>
      <div className="modal" onClick={(e) => e.stopPropagation()}>
        <button className="modal-close" onClick={onClose} aria-label="Close">
          <Icon name="x" size={20} />
        </button>
        <div className="modal-gallery">
          <div className="main">
            <img src={listing.images[idx]} alt={listing.address} />
            {listing.images.length > 1 && (
              <>
                <button className="arrow prev" onClick={() => setIdx(i => (i - 1 + listing.images.length) % listing.images.length)} aria-label="Previous">
                  <Icon name="chevron-left" size={22} />
                </button>
                <button className="arrow next" onClick={() => setIdx(i => (i + 1) % listing.images.length)} aria-label="Next">
                  <Icon name="chevron-right" size={22} />
                </button>
              </>
            )}
          </div>
          <div className="thumbs">
            {listing.images.map((src, i) => (
              <div key={i} className={`thumb ${i === idx ? 'active' : ''}`} onClick={() => setIdx(i)}>
                <img src={src} alt={`Photo ${i + 1}`} />
              </div>
            ))}
          </div>
        </div>
        <div className="modal-body">
          <div style={{ display: 'flex', gap: 10, marginBottom: 14 }}>
            <span className={`pill ${listing.status === 'sold' ? 'pill-sold' : 'pill-active'}`}>
              {listing.status === 'sold' ? 'Sold' : 'For Sale'}
            </span>
            <span className="pill pill-outline">{listing.type}</span>
            {listing.year && <span className="pill pill-outline">Built {listing.year}</span>}
          </div>
          <h2 className="serif">{formatFullPrice(listing.price)}</h2>
          <div style={{ marginTop: 6, fontSize: 17, color: 'var(--ink-2)' }}>{listing.address}</div>
          <div style={{ fontSize: 15, color: 'var(--ink-3)', marginTop: 4 }}>{listing.neighbourhood} · {listing.city}</div>

          <div className="modal-spec-row">
            <div className="modal-spec"><span className="l">Beds</span><span className="v">{listing.beds}</span></div>
            <div className="modal-spec"><span className="l">Baths</span><span className="v">{listing.baths}</span></div>
            <div className="modal-spec"><span className="l">Sq ft</span><span className="v">{listing.sqft.toLocaleString()}</span></div>
            <div className="modal-spec"><span className="l">Type</span><span className="v" style={{ fontSize: 18 }}>{listing.type}</span></div>
          </div>

          <p style={{ marginTop: 24, color: 'var(--ink-2)', lineHeight: 1.7 }}>{listing.description}</p>

          <div className="modal-features">
            {listing.features.map(f => <span key={f} className="pill pill-outline">{f}</span>)}
          </div>

          <div className="modal-actions">
            <button className="btn btn-primary" onClick={() => { onClose(); setRoute('contact'); }}>
              <Icon name="calendar" size={16} /> Request a showing
            </button>
            <a className="btn btn-ghost" href={`tel:${SITE_DATA.agent.phone}`}>
              <Icon name="phone" size={16} /> Call Steffanie
            </a>
          </div>
        </div>
      </div>
    </div>
  );
};
window.ListingModal = ListingModal;
