// Page chrome — Header, Footer, and small reusable bits

// Optional router context. When a provider is present (interactive prototype),
// nav clicks navigate. When absent (design canvas), links are inert.
const RouterCtx = React.createContext(null);
function useRouter() { return React.useContext(RouterCtx); }
function RouterProvider({ value, children }) {
  return <RouterCtx.Provider value={value}>{children}</RouterCtx.Provider>;
}

function PhoneNumber({ t, big, ink, mono }) {
  return (
    <span style={{
      fontFamily: mono ? "ui-monospace,Menlo,Consolas,monospace" : t.bFont,
      fontWeight: 600, fontSize: big ? 22 : 13, letterSpacing: .3,
      color: ink || t.ink, whiteSpace: 'nowrap'
    }}>0808 094 2210</span>
  );
}

function Header({ t, active = 'Home' }) {
  const nav = ['Home','Services','Gallery','About','Trade','Contact'];
  return (
    <header style={{
      background: t.bg, borderBottom: `1px solid ${t.line}`,
      padding: '14px 48px', display: 'flex', alignItems:'center', gap: 32,
      position:'sticky', top:0, zIndex: 5,
    }}>
      <div style={{ display:'flex', alignItems:'center', gap: 10 }}>
        <img src="assets/logo-black.png" alt="" style={{ width: 32, height: 32 }}/>
        <span style={{ fontFamily: t.hFont, fontSize: 19, letterSpacing: -.2, color: t.ink }}>
          Fin's Flooring
        </span>
      </div>
      <nav style={{ display:'flex', gap: 26, marginLeft: 18, flex: 1 }}>
        {nav.map(n => (
          <NavLink key={n} t={t} to={n} active={n===active}/>
        ))}
      </nav>
      <div style={{ display:'flex', alignItems:'center', gap: 14 }}>
        <span style={{ fontFamily: t.bFont, fontSize: 11.5, color: t.muted, letterSpacing:.4, textTransform:'uppercase' }}>Free quote</span>
        <PhoneNumber t={t} big />
        <CTAButton t={t} to="Contact">Call now</CTAButton>
      </div>
    </header>
  );
}

function MobileHeader({ t, active = 'Home' }) {
  const router = useRouter();
  const [menuOpen, setMenuOpen] = React.useState(false);
  const nav = ['Home','Services','Gallery','About','Trade','Contact'];
  return (
    <header style={{
      background: t.bg, borderBottom: `1px solid ${t.line}`,
      padding: '12px 16px', display:'flex', alignItems:'center', gap: 10,
      position:'sticky', top:0, zIndex: 5,
    }}>
      <div onClick={() => setMenuOpen(o => !o)} style={{ display:'flex', flexDirection:'column', gap:3, cursor:'pointer', padding: 4, margin: -4 }}>
        <span style={{ width: 20, height: 1.5, background: t.ink }}/>
        <span style={{ width: 20, height: 1.5, background: t.ink }}/>
        <span style={{ width: 20, height: 1.5, background: t.ink }}/>
      </div>
      <div style={{ display:'flex', alignItems:'center', gap: 8, flex: 1, justifyContent:'center' }}>
        <img src="assets/logo-black.png" alt="" style={{ width: 24, height: 24 }}/>
        <span style={{ fontFamily: t.hFont, fontSize: 15, letterSpacing:-.2, color: t.ink }}>Fin&rsquo;s Flooring</span>
      </div>
      <a onClick={() => router && router.go('Contact')} style={{
        fontFamily: t.bFont, fontSize: 11, fontWeight: 600, color: t.primary,
        textDecoration: 'none', cursor: router ? 'pointer' : 'default',
      }}>Call</a>
      {menuOpen && (
        <div style={{
          position:'absolute', top:'100%', left: 0, right: 0,
          background: t.bg, borderBottom: `1px solid ${t.line}`,
          padding: '8px 16px 16px', display:'flex', flexDirection:'column', gap: 2,
          zIndex: 10,
        }}>
          {nav.map(n => (
            <a key={n} onClick={() => { router && router.go(n); setMenuOpen(false); }} style={{
              fontFamily: t.bFont, fontSize: 16, fontWeight: 500,
              color: n === active ? t.primary : t.ink, padding: '10px 0',
              borderBottom: `1px solid ${t.line}`, cursor: router ? 'pointer' : 'default',
            }}>{n}</a>
          ))}
        </div>
      )}
    </header>
  );
}

// Nav link — uses router if present, otherwise inert text
function NavLink({ t, to, active }) {
  const router = useRouter();
  return (
    <a onClick={() => router && router.go(to)} style={{
      fontFamily: t.bFont, fontSize: 13.5, fontWeight: 500,
      color: active ? t.primary : t.body, textDecoration:'none',
      paddingBottom: 4,
      borderBottom: active ? `1.5px solid ${t.primary}` : '1.5px solid transparent',
      cursor: router ? 'pointer' : 'default',
    }}>{to}</a>
  );
}

// CTA button that navigates if router is present
function CTAButton({ t, to, children, style }) {
  const router = useRouter();
  return (
    <button onClick={() => router && router.go(to)} style={{...btnPrimary(t), cursor: router?'pointer':'default', ...style}}>
      {children}
    </button>
  );
}

function MobileBottomBar({ t }) {
  const router = useRouter();
  return (
    <div style={{
      position:'sticky', bottom: 0, padding: '10px 14px 14px',
      background: t.bg, borderTop: `1px solid ${t.line}`,
      display:'flex', gap: 8, zIndex: 4,
    }}>
      <button onClick={() => router && router.go('Contact')} style={{...btnPrimary(t), flex: 1, padding: '14px 14px', display:'flex', alignItems:'center', justifyContent:'center', gap:8, cursor: router?'pointer':'default' }}>
        <span style={{ width: 14, height: 14, borderRadius:'50%', background: t.primaryInk, opacity:.25 }}/>
        Call now — 0808 094 2210
      </button>
    </div>
  );
}

function Footer({ t, mobile }) {
  return (
    <footer style={{
      background: t.ink, color: t.bg,
      padding: mobile ? '36px 20px 28px' : '56px 48px 32px',
    }}>
      <div style={{
        display: mobile ? 'flex' : 'grid',
        gridTemplateColumns: mobile ? undefined : '1.4fr 1fr 1fr 1fr',
        flexDirection: mobile ? 'column' : undefined,
        gap: mobile ? 26 : 40,
      }}>
        <div>
          <div style={{ display:'flex', alignItems:'center', gap: 10, marginBottom: 14 }}>
            <img src="assets/logo-white.png" alt="" style={{ width: 36, height: 36 }}/>
            <span style={{ fontFamily: t.hFont, fontSize: 22, color: t.bg }}>Fin's Flooring</span>
          </div>
          <p style={{ fontFamily: t.bFont, fontSize: 13.5, lineHeight: 1.6, color: 'rgba(255,255,255,.7)', maxWidth: 320, margin: 0 }}>
            Supply, prep and install — laminate, LVT and carpet, fitted by our own teams nationwide.
          </p>
          <div style={{ marginTop: 18, fontFamily: t.bFont, fontSize: 13.5, color: 'rgba(255,255,255,.85)' }}>
            <div>0808 094 2210</div>
            <div style={{ opacity:.7 }}>hello@finsflooring.co.uk</div>
          </div>
        </div>
        <FooterCol t={t} title="Browse" items={['Home','Services','Gallery','About','Contact']}/>
        <FooterCol t={t} title="Floors" items={['Laminate','LVT / Vinyl','Carpet','Floor preparation']}/>
        <FooterCol t={t} title="Company" items={['Trade enquiries','Service area','Reviews','Instagram','Pinterest']}/>
      </div>
      <div style={{
        display:'flex', justifyContent:'space-between', flexWrap:'wrap', gap: 12,
        borderTop:'1px solid rgba(255,255,255,.12)', marginTop: mobile?28:48, paddingTop: 18,
        fontFamily: t.bFont, fontSize: 11.5, color:'rgba(255,255,255,.55)',
      }}>
        <span>© 2026 Fin's Flooring Ltd · Registered in England & Wales</span>
        <span>Crafted in {t.key === 'cosy' ? 'Yorkshire' : t.key === 'modern' ? 'London' : 'the South Downs'}</span>
      </div>
    </footer>
  );
}
function FooterCol({ t, title, items }) {
  return (
    <div>
      <div style={{ fontFamily: t.bFont, fontSize: 11, letterSpacing: 2, textTransform:'uppercase', color:'rgba(255,255,255,.55)', marginBottom: 14 }}>{title}</div>
      <ul style={{ listStyle:'none', padding: 0, margin: 0, display:'flex', flexDirection:'column', gap: 8 }}>
        {items.map(i => <li key={i} style={{ fontFamily: t.bFont, fontSize: 13.5, color:'rgba(255,255,255,.85)' }}>{i}</li>)}
      </ul>
    </div>
  );
}

// Section primitive
function Section({ t, bg, children, pad = '72px 48px', style }) {
  return (
    <section style={{
      background: bg || t.bg, padding: pad, ...style,
    }}>{children}</section>
  );
}

function Eyebrow({ t, color, children }) {
  return (
    <div style={{
      fontFamily: t.bFont, fontSize: 11, letterSpacing: 2.4, textTransform:'uppercase',
      color: color || t.primary, marginBottom: 12, fontWeight: 600,
    }}>{children}</div>
  );
}

function SectionTitle({ t, children, size = 44, color }) {
  return (
    <h2 style={{
      fontFamily: t.hFont, fontSize: size, lineHeight: 1.08, letterSpacing: -.6,
      color: color || t.ink, margin: 0, fontWeight: 500, textWrap:'pretty',
    }}>{children}</h2>
  );
}

function Rule({ t, color }) {
  return <div style={{ height:1, background: color || t.line, width: '100%' }}/>;
}

// Small star row
function Stars({ t, color, n = 5 }) {
  return (
    <span style={{ color: color || t.primary, letterSpacing: 2, fontSize: 14 }}>
      {Array.from({length:n}).map((_,i)=> <span key={i}>★</span>)}
    </span>
  );
}

Object.assign(window, { Header, MobileHeader, MobileBottomBar, Footer, Section, Eyebrow, SectionTitle, Rule, Stars, PhoneNumber, RouterCtx, RouterProvider, useRouter, NavLink, CTAButton });
