// Facility hire: Tilly's Bar, the clubhouse bar at Brews Park (#tillys-bar).
// Prices are on enquiry; enquiries go to the commercial inbox.
// Photos: assets/facilities/tillys-N-name.webp (+ -480 thumbnails).
// ---------------------------------------------------------------------------

const TILLYS = {
  email: 'commercial@blackpoolwrenrovers.co.uk',
  address: 'Brews Park, School Road, Blackpool FY4 5DS',
};

const TILLYS_PHOTOS = [
  ['tillys-1-bar', "The bar at Tilly's Bar, with bar stools and the club badge lit up behind"],
  ['tillys-2-bar-seating', 'The bar and tables with seating in front'],
  ['tillys-3-lounge', 'The full length of the room, with long tables, chairs and a bench along the wall'],
  ['tillys-4-darts-corner', 'Tables and chairs by the dartboard, with a TV on the far wall'],
  ['tillys-5-function-room', 'The seating area, with the club badge, trophies and a TV on the back wall'],
  ['tillys-6-badge-wall', 'Tables beside the feature wall with the large club badge'],
  ['tillys-7-badge-trophies', 'The club badge feature wall above the trophy shelf'],
].map(([file, alt]) => ({
  src: `assets/facilities/${file}.webp`,
  thumb: `assets/facilities/${file}-480.webp`,
  alt,
}));

const TILLYS_FEATURES = [
  ['Bar', 'A full bar with bar stools, right at the heart of the room.'],
  ['Seating', 'Tables and chairs throughout, plus long tables for bigger groups.'],
  ['Screens', 'TV screens around the room for the match or your own slideshow.'],
  ['Darts', 'A dartboard corner to keep everyone entertained.'],
];

const tillysMailto = () => {
  const subject = "Tilly's Bar hire enquiry";
  const body = [
    "Hi, I'd like to enquire about hiring Tilly's Bar.",
    '',
    'Date:',
    'Start and finish time:',
    'Number of guests:',
    'Type of event:',
    'Anything else:',
    '',
    'Name:',
    'Phone number:',
  ].join('\n');
  return `mailto:${TILLYS.email}?subject=${encodeURIComponent(subject)}&body=${encodeURIComponent(body)}`;
};

const Lightbox = ({ photos, index, onClose, onMove }) => {
  React.useEffect(() => {
    const onKey = (e) => {
      if (e.key === 'Escape') onClose();
      if (e.key === 'ArrowRight') onMove(1);
      if (e.key === 'ArrowLeft') onMove(-1);
    };
    document.addEventListener('keydown', onKey);
    const prev = document.body.style.overflow;
    document.body.style.overflow = 'hidden';
    return () => { document.removeEventListener('keydown', onKey); document.body.style.overflow = prev; };
  }, [onClose, onMove]);

  const p = photos[index];
  const box = (
    <div className="lightbox" role="dialog" aria-modal="true" aria-label="Photo viewer" onClick={onClose}>
      <button type="button" className="lightbox__close" aria-label="Close" onClick={onClose}>✕</button>
      <button type="button" className="lightbox__nav lightbox__nav--prev" aria-label="Previous photo"
        onClick={(e) => { e.stopPropagation(); onMove(-1); }}>‹</button>
      <figure className="lightbox__figure" onClick={(e) => e.stopPropagation()}>
        <img src={p.src} alt={p.alt} />
        <figcaption className="mono">{index + 1} / {photos.length}</figcaption>
      </figure>
      <button type="button" className="lightbox__nav lightbox__nav--next" aria-label="Next photo"
        onClick={(e) => { e.stopPropagation(); onMove(1); }}>›</button>
    </div>
  );
  return ReactDOM.createPortal(box, document.body);
};

const FacilitiesPage = () => {
  const [lb, setLb] = React.useState(null);
  const close = React.useCallback(() => setLb(null), []);
  const move = React.useCallback((d) => setLb(i => (i + d + TILLYS_PHOTOS.length) % TILLYS_PHOTOS.length), []);

  return (
    <>
      {/* Intro */}
      <section className="tb">
        <div className="container tb__grid">
          <div>
            <div className="mono hc__kicker">▸ AVAILABLE FOR PRIVATE HIRE</div>
            <h2 className="display tb__title">Book the bar.<br /><span>Your event, at Brews Park.</span></h2>
            <p className="tb__lede">
              Tilly's Bar is the clubhouse bar at Blackpool Wren Rovers, and it's available to hire.
              A proper bar, plenty of seating and screens around the room, all finished in the club's own red and black.
            </p>
            <p className="tb__lede tb__lede--soft">
              Get in touch to check dates and prices for birthdays, parties, celebrations and community events.
            </p>
            <div className="tb__actions">
              <Btn variant="primary" size="lg" as="a" href={tillysMailto()}>Enquire about hire</Btn>
              <a className="tb__email" href={tillysMailto()}>{TILLYS.email}</a>
            </div>
          </div>
          <button type="button" className="tb__hero" onClick={() => setLb(2)} aria-label="Open photo of the room">
            <img src={TILLYS_PHOTOS[2].src} alt={TILLYS_PHOTOS[2].alt} />
            <div className="tb__price">
              <div className="mono">PRICES</div>
              <div className="display">On enquiry</div>
            </div>
          </button>
        </div>
        <StripeRule height={5} />
      </section>

      {/* What's in the room */}
      <section style={{ background: 'var(--paper)', padding: '80px 0 24px' }}>
        <div className="container">
          <SectionHead kicker="The Room" title="What's included." />
          <ul className="tb__features">
            {TILLYS_FEATURES.map(([t, b], i) => (
              <li key={t}>
                <span className="display hc__stepNo">{String(i + 1).padStart(2, '0')}</span>
                <div className="display hc__stepTitle">{t}</div>
                <p>{b}</p>
              </li>
            ))}
          </ul>
        </div>
      </section>

      {/* Gallery */}
      <section style={{ background: 'var(--paper)', padding: '56px 0 40px' }}>
        <div className="container">
          <SectionHead kicker="Gallery" title="Take a look inside."
            right={<p style={{ maxWidth: 340, margin: 0, color: 'var(--muted)', fontSize: 14, lineHeight: 1.55 }}>Tap any photo to see it full size.</p>} />
          <div className="tb__gallery">
            {TILLYS_PHOTOS.map((p, i) => (
              <button type="button" key={p.src} className="tb__tile" onClick={() => setLb(i)} aria-label={'Open photo: ' + p.alt}>
                <img src={p.thumb} alt={p.alt} loading="lazy" decoding="async" />
              </button>
            ))}
          </div>
        </div>
      </section>

      {/* Enquire */}
      <section style={{ background: 'var(--paper)', padding: '40px 0 96px' }}>
        <div className="container">
          <div className="tb__enquire">
            <div>
              <div className="mono hc__kicker">▸ HOW TO BOOK</div>
              <div className="display tb__enquireTitle">Prices on enquiry.</div>
              <p>
                Email us with the date you're after, roughly how many guests and what the occasion is,
                and we'll come back to you with availability and prices.
              </p>
              <p className="mono tb__addr">{TILLYS.address.toUpperCase()}</p>
            </div>
            <div className="tb__enquireSide">
              <a className="tb__email tb__email--big" href={tillysMailto()}>{TILLYS.email}</a>
              <Btn variant="primary" size="lg" as="a" href={tillysMailto()}>Email an enquiry</Btn>
            </div>
          </div>
        </div>
      </section>

      {lb !== null && <Lightbox photos={TILLYS_PHOTOS} index={lb} onClose={close} onMove={move} />}
    </>
  );
};

Object.assign(window, { FacilitiesPage, Lightbox });
