import type { Course, Setting } from '@/payload-types'
import { ContactBlock } from '@/components/Contact'
import { type Locale, isExternal, localHref, other } from '@/lib/site'

type Props = {
  locale: Locale
  /** this page's path without the language prefix ("/", "/courses", "/courses/bacc-en") */
  path: string
  settings: Setting
  courses: Course[]
  children: React.ReactNode
}

/** A globe: the language switch says what it does without a word that has to be translated. */
function GlobeIcon() {
  return (
    <svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" strokeWidth="1.8"
         strokeLinecap="round" aria-hidden="true" focusable="false">
      <circle cx="12" cy="12" r="9" />
      <path d="M3 12h18M12 3c2.5 2.6 2.5 15.4 0 18M12 3c-2.5 2.6-2.5 15.4 0 18" />
    </svg>
  )
}

/** The three lines every phone user already reads as "menu". */
function MenuIcon() {
  return (
    <svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" strokeWidth="2"
         strokeLinecap="round" aria-hidden="true" focusable="false">
      <path d="M4 7h16M4 12h16M4 17h16" />
    </svg>
  )
}

/** Header, main content and footer: the frame every page shares. */
export function Shell({ locale, path, settings: s, courses, children }: Props) {
  const brand = s.brandName || 'Thinkera'
  const alt = other(locale)
  const current = (href: string) => (href === '/' ? path === '/' : path === href || path.startsWith(`${href}/`))
  return (
    <>
      <a className="skip" href="#main">
        {s.ui?.skip}
      </a>
      <header className="site-header">
        <div className="wrap">
          <a className="brand-link" href={localHref(locale, '/')} aria-label={brand}>
            <picture>
              <source srcSet="/brand/thinkera-logo-white.svg" media="(prefers-color-scheme: dark)" />
              <img src="/brand/thinkera-logo.svg" alt={brand} width={122} height={34} />
            </picture>
          </a>
          <nav className="nav" id="nav" aria-label={s.nav?.[0]?.label || brand}>
            <ul>
              {(s.nav || []).map((n) => (
                <li key={n.id || n.href}>
                  <a href={localHref(locale, n.href)} aria-current={current(n.href) ? 'page' : undefined}>
                    {n.label}
                  </a>
                </li>
              ))}
              {s.platformUrl ? (
                <li className="nav-signin">
                  <a href={s.platformUrl} rel="noopener">
                    {s.ui?.signIn}
                  </a>
                </li>
              ) : null}
            </ul>
          </nav>
          <div className="header-tools">
            <a
              className="btn btn--quiet btn--icon"
              href={localHref(alt, path)}
              hrefLang={alt}
              lang={alt}
              aria-label={s.ui?.langSwitch || (alt === 'en' ? 'English' : 'العربية')}
              title={s.ui?.langSwitch || undefined}
            >
              <GlobeIcon />
              <span aria-hidden="true">{alt === 'en' ? 'EN' : 'ع'}</span>
            </a>
            {s.platformUrl ? (
              <a className="btn btn--quiet header-signin" href={s.platformUrl} rel="noopener">
                {s.ui?.signIn}
              </a>
            ) : null}
            <button
              className="btn btn--quiet btn--icon nav-toggle"
              type="button"
              aria-expanded="false"
              aria-controls="nav"
              aria-label={s.ui?.menu || undefined}
            >
              <MenuIcon />
              <span className="nav-toggle-label" aria-hidden="true">
                {s.ui?.menu}
              </span>
            </button>
            <a className="btn btn--primary" href={localHref(locale, s.freeLessonHref)}>
              {s.cta?.free}
            </a>
          </div>
        </div>
      </header>
      {children}
      <footer className="site-footer">
        <div className="wrap">
          <div className="cols">
            <div>
              <img src="/brand/thinkera-logo-white.svg" alt={brand} width={150} height={42} />
              <p style={{ marginTop: 'var(--tk-space-4)', maxWidth: '34ch' }}>{s.footer?.tagline}</p>
              <ContactBlock settings={s} />
            </div>
            <div>
              <h2>{s.footer?.coursesHeading}</h2>
              <ul>
                {courses.map((c) => (
                  <li key={c.id}>
                    <a href={localHref(locale, `/courses/${c.slug}`)}>{c.title}</a>
                  </li>
                ))}
              </ul>
            </div>
            <div>
              <h2>{s.footer?.academyHeading}</h2>
              <ul>
                {(s.footer?.links || []).map((l) => (
                  <li key={l.id || l.href}>
                    <a href={localHref(locale, l.href)} rel={isExternal(l.href) ? 'noopener' : undefined}>
                      {l.label}
                    </a>
                  </li>
                ))}
              </ul>
            </div>
          </div>
          <div className="legal">
            <span>
              © <span className="num">{new Date().getFullYear()}</span> {brand}. {s.footer?.rights}
            </span>
          </div>
        </div>
      </footer>
    </>
  )
}
