// Footer — Layer 1, v7: edge-to-edge 5-column band.
//
// Brief from the client:
//   · Use BOTH ends (no centred narrow island)
//   · Minimum possible height
//   · Tighter line-height
//   · Map sits NEXT TO Visit (separate column), not stacked under it
//   · Cleanly collapses on small screens
//
// How v7 delivers that:
//   · 5 columns: LOGO · VISIT · MAP · CONTACT · STUDIO. The grid
//     reaches a wider container (1480 px) and the outer padding
//     uses the full viewport edges, so the row sits ear-to-ear
//     instead of clustering in the middle.
//   · `align-items: center` on the grid — every column is
//     vertically centred against the tallest item (the map). Means
//     the footer's overall height is just the map height plus
//     padding; nothing pads it out unnecessarily.
//   · Line-heights pulled down (lists 1.45, address 1.5), gaps
//     between list items reduced from 10 → 4 px.
//   · Heading-to-body gap reduced from 14 → 8 px.
//   · Responsive collapse — clearly defined breakpoints:
//       ≥ 1024 px  →  5 cols (logo | visit | map | contact | studio)
//       720–1023   →  2 rows: logo+visit+map on top, contact+studio
//                      below — map sits next to address as before
//       560–719    →  2 cols (visit | map) (contact | studio),
//                      logo spans full width above
//       < 560      →  single column, map full-width

const { useState } = React;

// ─── Company facts ─────────────────────────────────────────────────
const COMPANY = {
  trading:     'Venoraa Ceramics',
  registered:  'The Tiles Company Ltd',
  addrLine1:   '64 Weir Road',
  addrLine2:   'Wimbledon, London',
  postcode:    'SW19 8UG',
  phoneLondon: '020 3441 4711',
  phoneMobile: '07830 385 556',
  email:       'info@thetilescompany.com',
  instagram:   'https://www.instagram.com/thetilescompany?igsh=YXFibG1vcThkb3Fl',
};

// Map locked to the specific Venoraa Ceramics / The Tiles Company
// business listing on Google Maps. The previous address-only query
// was picking up a different shop at the same street block and
// pulling its (wrong) photos into the embed preview.
//
// MAP_COORDS pins the embed to the exact lat/lng from the studio's
// Google Maps listing — guaranteed correct location, immune to any
// future re-geocoding mistakes.
// MAP_LINK is the studio's own Google Maps share URL, so the
// "Directions" overlay opens THIS business's page (with photos,
// reviews, hours) rather than a generic address search.
const MAP_COORDS = '51.4378929,-0.1900138';
const MAP_SRC    = `https://maps.google.com/maps?q=${MAP_COORDS}&z=17&output=embed`;
const MAP_LINK   = 'https://maps.app.goo.gl/v74ntM6wDccQe8pv7';

// Single source for the map's pixel height. Drives the iframe
// over-render math too — every other column ends up vertically
// centred against this dimension, so changing it rebalances the
// whole row.
const MAP_H = 112;

function Footer({ navigate }) {

  return (
    <footer style={{
      position: 'relative',
      marginTop: '40px',
      background: '#FFFFFF',
      borderTop: '1px solid rgba(15,15,15,0.06)',
      color: 'var(--dark)',
      overflow: 'hidden',
    }}>
      {/* Soft ambient wash. */}
      <div aria-hidden style={{
        position: 'absolute', inset: 0, zIndex: 0,
        background:
          'radial-gradient(ellipse 80% 120% at 50% 50%, rgba(240,240,240,0.55) 0%, rgba(248,248,248,0) 70%)',
        filter:        'blur(6px)',
        pointerEvents: 'none',
      }}/>

      <div style={{
        position: 'relative', zIndex: 1,
        // Wider container so the row reaches both edges of the page.
        maxWidth: '1480px',
        margin: '0 auto',
        // Minimum possible vertical padding while leaving the row a
        // touch of breathing room above + below.
        padding: 'clamp(18px, 2vw, 26px) clamp(18px, 3vw, 40px) 12px',
      }}>

        {/* ── 5-column band, edge to edge ─────────────────────────
             Order (left → right): LOGO · STUDIO · CONTACT · VISIT · MAP
             Map gets the largest fractional unit and NO max-width
             so it stretches all the way to the right edge of the
             container — anchoring the right side the way the logo
             anchors the left. */}
        <div className="footer-cols" style={{
          display: 'grid',
          // Studio · Contact · Visit all share the same 1fr so the
          // three text columns are visually identical in width and
          // the eye reads them as a uniform set rather than three
          // slightly-different sizes. Map gets the largest share.
          gridTemplateColumns:
            'auto minmax(120px, 1fr) minmax(120px, 1fr) minmax(120px, 1fr) minmax(260px, 1.7fr)',
          columnGap: 'clamp(20px, 2.6vw, 44px)',
          rowGap: 0,
          // `start` — every column's TOP edge aligns to the same
          // horizontal line, which means all three text headings
          // (STUDIO / CONTACT / VISIT) sit on the same baseline
          // regardless of how many lines of body content each
          // column carries. The logo and the map also start at the
          // same top edge, so the band reads as a unified row.
          alignItems: 'start',
        }}>

          {/* ── 1 · LOGO (left anchor) ───────────────────────────
               paddingRight on the anchor widens the auto-sized
               logo cell so Studio doesn't sit flush up against
               the wordmark. The Studio→Contact and other
               column-to-column gaps use the grid's columnGap;
               this padding adds the equivalent breathing room on
               the logo side so all visual gaps read as similar. */}
          <a href="#top" onClick={(e) => { e.preventDefault(); navigate('home'); }}
            style={{
              display: 'inline-flex', alignItems: 'center', textDecoration: 'none',
              paddingRight: 'clamp(16px, 3vw, 48px)',
            }}>
            <img
              src="/assets/brand/venoraa-logo.svg"
              alt="Venoraa Ceramics"
              style={{ height: '54px', width: 'auto', display: 'block' }}
            />
          </a>

          {/* ── 2 · STUDIO ─────────────────────────────────────── */}
          <FooterColumn title="Studio">
            <ul style={listReset}>
              <li style={listItem}><button onClick={() => navigate('collections')} style={footerLink}>Collections</button></li>
              <li style={listItem}><button onClick={() => navigate('journal')}     style={footerLink}>Journal</button></li>
              <li style={listItem}><button onClick={() => navigate('quote')}       style={footerLink}>Request a quote</button></li>
              <li style={listItem}><button onClick={() => navigate('trade')}       style={footerLink}>Trade enquiries</button></li>
            </ul>
          </FooterColumn>

          {/* ── 3 · CONTACT ────────────────────────────────────── */}
          <FooterColumn title="Contact">
            <ul style={listReset}>
              <li style={listItem}>
                <a href={`tel:${COMPANY.phoneLondon.replace(/\s+/g, '')}`} style={footerLink}>
                  {COMPANY.phoneLondon}
                </a>
              </li>
              <li style={listItem}>
                <a href={`tel:${COMPANY.phoneMobile.replace(/\s+/g, '')}`} style={footerLink}>
                  {COMPANY.phoneMobile}
                </a>
              </li>
              <li style={listItem}>
                <a href={`mailto:${COMPANY.email}`} style={footerLink}>
                  {COMPANY.email}
                </a>
              </li>
              <li style={listItem}>
                <a href={COMPANY.instagram} target="_blank" rel="noopener noreferrer" style={footerLink}>
                  Instagram
                </a>
              </li>
            </ul>
          </FooterColumn>

          {/* ── 4 · VISIT ──────────────────────────────────────── */}
          <FooterColumn title="Visit">
            <address style={{ ...bodyText, fontStyle: 'normal' }}>
              {/* Trading name sits above the postal address so the
                  visitor can pair "what's it called" with "where is
                  it" at a glance — and so the studio's brand reads
                  before the street, not after. */}
              <span style={{ fontWeight: 500, color: 'var(--dark)' }}>
                {COMPANY.trading}
              </span><br/>
              {COMPANY.addrLine1}<br/>
              {COMPANY.addrLine2}<br/>
              {COMPANY.postcode}
            </address>
          </FooterColumn>

          {/* ── 5 · MAP (right anchor) ─────────────────────────────
               Fills its grid cell — no maxWidth — so it reaches
               the container's right edge cleanly. Iframe is
               over-rendered + offset to clip Google's stock
               corner widgets out of view. */}
          <div className="footer-map" style={{
            position: 'relative',
            width: '100%',
            height: `${MAP_H}px`,
            border: '1px solid rgba(15,15,15,0.08)',
            overflow: 'hidden',
            background: 'var(--cream-deep)',
            justifySelf: 'stretch',
          }}>
            <iframe
              title="Venoraa Ceramics — 64 Weir Road, Wimbledon"
              src={MAP_SRC}
              loading="lazy"
              referrerPolicy="no-referrer-when-downgrade"
              style={{
                position: 'absolute',
                top: '-40px', left: '-58px',
                width: 'calc(100% + 116px)',
                height: 'calc(100% + 80px)',
                border: 0,
                filter: 'saturate(0.82) contrast(0.96)',
                pointerEvents: 'none',
              }}
              allowFullScreen
            />
            <a href={MAP_LINK} target="_blank" rel="noopener noreferrer"
              aria-label="Open in Google Maps"
              style={{
                position: 'absolute', inset: 0,
                display: 'flex', alignItems: 'flex-end', justifyContent: 'flex-end',
                padding: '7px 9px',
                textDecoration: 'none',
              }}>
              <span style={{
                background: 'rgba(15,15,15,0.82)',
                color: 'white',
                fontFamily: 'var(--sans)', fontSize: '10px', fontWeight: 500,
                letterSpacing: '0.14em', textTransform: 'uppercase',
                padding: '4px 8px',
              }}>
                Directions →
              </span>
            </a>
          </div>
        </div>

        {/* ── Bottom legal bar ─────────────────────────────────── */}
        <div className="footer-legal" style={{
          marginTop: 'clamp(14px, 1.6vw, 22px)',
          paddingTop: '12px',
          borderTop: '1px solid rgba(15,15,15,0.06)',
          display: 'flex',
          justifyContent: 'space-between',
          alignItems: 'center',
          gap: '6px 20px',
          flexWrap: 'wrap',
          fontFamily: 'var(--sans)', fontSize: '11px',
          color: 'var(--dark-mid)', letterSpacing: '0.05em',
        }}>
          <div className="footer-legal-left" style={{
            display: 'flex', flexWrap: 'wrap', gap: '3px 12px',
            alignItems: 'center',
          }}>
            <span style={{ color: 'var(--dark)' }}>{COMPANY.trading}</span>
            <Sep/>
            <span>{COMPANY.registered}</span>
            <Sep/>
            <button onClick={() => navigate('privacy')} style={{
              background: 'none', border: 'none', cursor: 'pointer', padding: 0,
              fontFamily: 'inherit', fontSize: 'inherit',
              color: 'var(--dark-mid)',
              letterSpacing: '0.05em',
            }}>
              Privacy notice
            </button>
          </div>
          <span style={{ whiteSpace: 'nowrap' }}>
            © {new Date().getFullYear()} The Tiles Company Ltd
          </span>
        </div>
      </div>

      {/* ── Responsive collapses ─────────────────────────────────
           Child order in the DOM is: 1=Logo · 2=Studio · 3=Contact
           · 4=Visit · 5=Map. The breakpoint rules reflow that order
           into 2-row / 3-row stacks rather than fighting it. */}
      <style>{`
        /* Tablet (720–1023): 2 rows.
             Row 1: Logo · Studio · Contact
             Row 2: Visit · Map  (map keeps its place next to Visit,
                    spans the remaining cell so it stays prominent) */
        @media (max-width: 1023px) {
          .footer-cols {
            grid-template-columns: auto minmax(110px, 1fr) minmax(140px, 1fr) !important;
            row-gap: 24px !important;
          }
          /* Row 2 placement */
          .footer-cols > :nth-child(4) { /* Visit */
            grid-column: 1 / 2 !important;
          }
          .footer-cols > :nth-child(5) { /* Map */
            grid-column: 2 / 4 !important;
            justify-self: stretch;
          }
        }
        /* Large phone (460–719): logo on its own row, then 2x2
           below (Studio · Contact / Visit · Map). */
        @media (max-width: 719px) {
          .footer-cols {
            grid-template-columns: 1fr 1fr !important;
            row-gap: 22px !important;
          }
          .footer-cols > :nth-child(1) { /* Logo full width */
            grid-column: 1 / -1 !important;
            justify-self: start;
          }
          .footer-cols > :nth-child(2),
          .footer-cols > :nth-child(3),
          .footer-cols > :nth-child(4),
          .footer-cols > :nth-child(5) {
            grid-column: auto !important;
            justify-self: stretch;
          }
        }
        /* Phone — fully stacked. */
        @media (max-width: 460px) {
          .footer-cols {
            grid-template-columns: 1fr !important;
            row-gap: 18px !important;
          }
          .footer-cols > * {
            grid-column: 1 / -1 !important;
          }
          .footer-legal {
            justify-content: flex-start !important;
          }
        }
      `}</style>
    </footer>
  );
}

// ─── Column primitive ──────────────────────────────────────────────
// Tight header → body rhythm (8 px gap, was 14).
function FooterColumn({ title, children }) {
  return (
    <div>
      <p style={headingText}>{title}</p>
      <div style={{ marginTop: '8px' }}>
        {children}
      </div>
    </div>
  );
}

function Sep() {
  return <span aria-hidden style={{ color: 'rgba(15,15,15,0.22)' }}>·</span>;
}

// ─── Shared styles ─────────────────────────────────────────────────
const headingText = {
  fontFamily: 'var(--sans)', fontSize: '10.5px', fontWeight: 500,
  letterSpacing: '0.18em', textTransform: 'uppercase',
  color: 'var(--terracotta)',
  lineHeight: 1,
  margin: 0,
};

const bodyText = {
  fontFamily: 'var(--sans)', fontSize: '13px', fontWeight: 400,
  lineHeight: 1.5,       // was 1.7 — tighter, fewer empty pixels
  color: 'var(--dark)',
  margin: 0,
};

const listReset = {
  listStyle: 'none', margin: 0, padding: 0,
};
// List items carry NO marginBottom — line rhythm is created
// entirely by lineHeight on the link itself, so each list item
// stacks on the same vertical step as the address lines in the
// Visit column (where bodyText sets lineHeight: 1.5).
const listItem = { margin: 0 };

const footerLink = {
  background: 'none', border: 'none', cursor: 'pointer', padding: 0,
  fontFamily: 'var(--sans)', fontSize: '13px', fontWeight: 400,
  color: 'var(--dark)',
  textDecoration: 'none',
  letterSpacing: '0.01em',
  textAlign: 'left',
  // Matches bodyText.lineHeight (Visit address) so Studio /
  // Contact / Visit all step at exactly the same vertical rhythm.
  lineHeight: 1.5,
  transition: 'color 0.2s ease, opacity 0.2s ease',
  display: 'inline-block',  // lineHeight only applies cleanly when block-ish
};

Object.assign(window, { Footer });
