"""Build the four new META 2027 pages from the shell of an existing one.

Nothing is invented and nothing is overwritten. programme.html supplies the head, the nav, the
footer and the closing script verbatim; only the metadata, the hero and the body between them are
replaced. The hero uses a class that already exists in meta27.css, so no stylesheet changes.

What is deliberately absent:
  * the ~400 confirmed invited speakers of META 2026, dropped from the symposia source — nobody
    has been re-invited, and publishing them under a META 2027 heading would announce a lie;
  * the Okamoto tutorial, which is real and already 2027-worded in OJS, but which Said asked to
    hold back until the tutorial program is settled.
"""

import html
import json
import re

SHELL = 'programme.html'
shell = open(SHELL, encoding='utf-8').read()

HERO_RE = re.compile(r'<header class="sub-hero[^"]*">.*?</header>', re.S)
BODY_START = shell.index('</header>') + len('</header>')
BODY_END = shell.index('<footer')


def esc(s):
    return html.escape(s, quote=True)


def page(slug, title, description, kicker, h1, lead, body, hero='hero-programme'):
    out = shell
    out = re.sub(r'<title>.*?</title>', '<title>%s</title>' % esc(title), out, count=1, flags=re.S)
    out = re.sub(r'(<meta name="description" content=")[^"]*(")',
                 lambda m: m.group(1) + esc(description) + m.group(2), out, count=1)
    out = re.sub(r'(<link rel="canonical" href="https://metaconferences\.org/META27/)[^"]*(")',
                 lambda m: m.group(1) + slug + m.group(2), out, count=1)
    out = re.sub(r'(<meta property="og:url" content="https://metaconferences\.org/META27/)[^"]*(")',
                 lambda m: m.group(1) + slug + m.group(2), out, count=1)
    for prop in ('og:title', 'twitter:title'):
        out = re.sub(r'(<meta (?:property|name)="%s" content=")[^"]*(")' % prop,
                     lambda m: m.group(1) + esc(title) + m.group(2), out, count=1)
    for prop in ('og:description', 'twitter:description'):
        out = re.sub(r'(<meta (?:property|name)="%s" content=")[^"]*(")' % prop,
                     lambda m: m.group(1) + esc(description) + m.group(2), out, count=1)

    hero_html = (
        '<header class="sub-hero %s">\n  <div class="wrap">\n'
        '    <p class="kicker">%s</p>\n    <h1>%s</h1>\n    <p class="lead">%s</p>\n'
        '  </div>\n</header>' % (hero, kicker, h1, lead)
    )
    out = HERO_RE.sub(lambda _: hero_html, out, count=1)
    start = out.index('</header>') + len('</header>')
    end = out.index('<footer')
    return out[:start] + '\n\n' + body + '\n\n' + out[end:]


# ---------------------------------------------------------------- Important Dates

ROWS = [
    ('September 1, 2026', 'Call for Papers opens', ''),
    ('February 12, 2027', 'Abstract submission deadline', '23:00 CET'),
    ('February 19, 2027', 'Acceptance notification', ''),
    ('April 23, 2027', 'Early bird registration deadline', '23:00 CET'),
    ('June 5, 2027', 'Advanced registration deadline', 'every presenting author must be registered by this date'),
    ('2June 5, 2027', 'Full-paper submission deadline', '23:00 CET'),
    ('July 13 &ndash; 16, 2027', 'META 2027 &mdash; Niagara Falls Convention Centre', ''),
]

rows = '\n'.join(
    '        <tr><td class="when">%s</td><td><strong>%s</strong>%s</td></tr>'
    % (d, e, '<br><span class="muted">%s</span>' % n if n else '')
    for d, e, n in ROWS
)

dates_body = """<section>
  <div class="wrap">
    <p class="kicker reveal">Calendar</p>
    <h2 class="sec reveal">The road to Niagara Falls</h2>
    <table class="dates-table reveal" style="width:100%%;border-collapse:collapse;margin-top:32px">
      <tbody>
%s
      </tbody>
    </table>
    <p class="reveal" style="margin-top:28px">Deadlines are firm. The submission portal stays open
      after the abstract deadline for invited papers and late poster submissions, at the discretion
      of the Technical Program Committee.</p>
    <p class="reveal">Questions about a date: <a href="mailto:contact@metaconferences.org">contact@metaconferences.org</a>.</p>
  </div>
</section>""" % rows

# ---------------------------------------------------------------- Accommodation

accommodation_body = """<section>
  <div class="wrap">
    <p class="kicker reveal">Attend</p>
    <h2 class="sec reveal">To be announced</h2>
    <p class="reveal" style="margin-top:24px">Hotel information and reserved room blocks for META 2027
      will be published on this page. Nothing has been arranged yet, and no hotel is currently
      authorized to take bookings on behalf of the conference.</p>

    <div class="info-card reveal" style="margin-top:36px;border-left:4px solid #d67c1c">
      <h3>Important notice &mdash; beware of housing scams</h3>
      <p><strong>META has no official housing partners.</strong> Companies claiming to hold rooms for
        META delegates, or asking for payment to secure accommodation, are not acting for the
        conference. If you are contacted by one, please write to
        <a href="mailto:contact@metaconferences.org">contact@metaconferences.org</a>.</p>
    </div>

    <p class="reveal" style="margin-top:36px">In the meantime, see
      <a href="attend.html">Attend</a> for travel and entry information, and
      <a href="visa.html">Visa &amp; Entry</a> if you need a Canadian eTA or visa.</p>
  </div>
</section>"""

# ---------------------------------------------------------------- Tutorials

tutorials_body = """<section>
  <div class="wrap">
    <p class="kicker reveal">Program</p>
    <h2 class="sec reveal">To be announced</h2>
    <p class="reveal" style="margin-top:24px">META 2027 will offer several technical tutorials given by
      experts from across the field, on topics chosen for the breadth of the META community. The
      tutorial program will be published on this page once it is settled.</p>
    <p class="reveal">Tutorials are part of the technical program and are
      <strong>included in the conference registration</strong> at no additional cost.</p>

    <div class="info-card reveal" style="margin-top:36px">
      <h3>Who they are for</h3>
      <ul>
        <li>Students and newcomers to the field</li>
        <li>Researchers and educators broadening their range</li>
        <li>Industry professionals looking for practical applications</li>
        <li>Program officers and funding agency representatives</li>
      </ul>
    </div>
  </div>
</section>"""

# ---------------------------------------------------------------- Symposia

syms = json.load(open('symposia.json', encoding='utf-8'))
blocks = []
for s in syms:
    title = s['title']
    chairs = s['chairs']
    heading = 'Symposium Chair' if len(chairs) == 1 else 'Symposium Co-Chairs'
    chair_html = '\n'.join(
        '          <li><strong>%s</strong><br><span class="muted">%s</span></li>' % (esc(n), esc(a))
        for n, a in chairs
    )
    desc_html = '\n'.join('      <p class="reveal">%s</p>' % esc(d) for d in s['desc'])
    topics_html = '\n'.join('          <li>%s</li>' % esc(t) for t in s['topics'])
    blocks.append("""  <div class="wrap" style="margin-top:56px">
    <h2 class="sec reveal">%s</h2>
%s
    <div class="two-col" style="margin-top:28px">
      <div class="info-card reveal">
        <h3>%s</h3>
        <ul>
%s
        </ul>
      </div>
      <div class="info-card reveal">
        <h3>Topics</h3>
        <ul>
%s
        </ul>
      </div>
    </div>
  </div>""" % (esc(title), desc_html, heading, chair_html, topics_html))

symposia_body = """<section>
  <div class="wrap">
    <p class="kicker reveal">Program</p>
    <h2 class="sec reveal">Six special symposia</h2>
    <p class="reveal" style="margin-top:24px">Each symposium is organized by its co-chairs around a
      focused theme, and runs within the main program. <strong>Invited speakers will be announced
      once the invitations have been confirmed.</strong></p>
    <p class="reveal">To propose a symposium or a special session for META 2027, see
      <a href="special-sessions.html">Special Sessions</a>.</p>
  </div>
</section>

<section>
%s
</section>""" % '\n\n'.join(blocks)

# ---------------------------------------------------------------- write

PAGES = [
    ('dates.html', 'Important Dates — META 2027 · Niagara Falls, July 13–16, 2027',
     'Key dates for META 2027: abstract deadline February 12, 2027, acceptance notification '
     'February 19, 2027, early registration until April 23, 2027, conference July 13–16, 2027.',
     'Program', 'Important dates',
     'Every deadline for META 2027, from the call for papers to the conference itself.',
     dates_body, 'hero-programme'),
    ('accommodation.html', 'Accommodation — META 2027 · Niagara Falls, Canada',
     'Accommodation information for META 2027 in Niagara Falls will be published here. '
     'META has no official housing partners.',
     'Attend', 'Where to stay',
     'Hotel information for META 2027 will be published here.',
     accommodation_body, 'hero-about'),
    ('tutorials.html', 'Tutorials — META 2027 · Technical Tutorials',
     'META 2027 will offer technical tutorials, included in the conference registration. '
     'The tutorial program will be announced.',
     'Program', 'Conference tutorials',
     'Technical tutorials given by experts from across the field, included in your registration.',
     tutorials_body, 'hero-programme'),
    ('symposia.html', 'Special Symposia — META 2027 · Niagara Falls',
     'The six special symposia of META 2027, their chairs and their topics. '
     'Invited speakers will be announced once confirmed.',
     'Program', 'Special symposia',
     'Six focused symposia within the META 2027 program, each shaped by its organizers.',
     symposia_body, 'hero-programme'),
]

for slug, title, desc, kicker, h1, lead, body, hero in PAGES:
    out = page(slug, title, desc, kicker, h1, lead, body, hero)
    open(slug, 'w', encoding='utf-8').write(out)
    print('%-22s %6d octets' % (slug, len(out)))
