# -*- coding: utf-8 -*-
"""Reorganiza o menu em todos os arquivos que o contêm.
- 'Sobre nós' -> 'O Restaurante' (mantém /sobre-nos/)
- insere item 'Eventos' -> /eventos/ após 'O Restaurante'
- remove o item 'Cardápio' do nav (link SAIPOS)
- botões laterais: iFood -> vira 'Cardápio' (SAIPOS); Reservar -> removido
"""
import re, os

SRC = "/config/workspace/Buenas-New/src"
FILES = ["_header.html", "home-body.html", "sobre-nos-body.html", "contato-body.html"]

EVENTOS_LI = ('<li class="menu-item menu-item-type-post_type menu-item-object-page">'
              '<a href="/eventos/" class="elementor-item">Eventos</a></li>')

CARDAPIO_BTN = (
    '<a class="elementor-button elementor-button-link elementor-size-sm" '
    'href="https://buenascarnespasta.saipos.com/home" target="_blank" rel="noopener">'
    '<span class="elementor-button-content-wrapper">'
    '<span class="elementor-button-icon">'
    '<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none">'
    '<path d="M4 3v6a2 2 0 0 0 2 2 2 2 0 0 0 2-2V3M6 11v10M18 3c-1.7 0-3 2.2-3 5 0 2.4 1 4.4 2.4 4.9V21" '
    'stroke="#F6F4F2" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"></path></svg></span>'
    '<span class="elementor-button-text">Cardápio</span></span></a>')

R_RENAME = re.compile(r'(href="/sobre-nos/"[^>]*>)Sobre nós(</a>)')
R_INSERT = re.compile(r'(<li[^>]*><a href="/sobre-nos/"[^>]*>[^<]*</a></li>)')
R_CARDLI = re.compile(r'<li[^>]*><a[^>]*href="https://buenascarnespasta\.saipos\.com/home"[^>]*'
                      r'class="elementor-item"[^>]*>Cardápio</a></li>')
R_IFOOD  = re.compile(r'<a class="elementor-button[^"]*"[^>]*href="https://www\.ifood\.com\.br/[^"]*"[^>]*>.*?</a>',
                      re.S)
R_RESERV = re.compile(r'<div class="elementor-element [^"]*elementor-widget-button"[^>]*>\s*'
                      r'<div class="elementor-widget-container">\s*<div class="elementor-button-wrapper">\s*'
                      r'<a[^>]*href="https://reservation-widget\.tagme\.com\.br/[^"]*"[^>]*>.*?</a>\s*'
                      r'</div>\s*</div>\s*</div>', re.S)

for fn in FILES:
    p = os.path.join(SRC, fn)
    t = open(p, encoding="utf-8").read()
    c = {}
    t, c["rename"]   = R_RENAME.subn(r'\1O Restaurante\2', t)
    t, c["eventos"]  = R_INSERT.subn(r'\1' + EVENTOS_LI, t)
    t, c["card_nav"] = R_CARDLI.subn('', t)
    t, c["ifood"]    = R_IFOOD.subn(CARDAPIO_BTN, t)
    t, c["reservar"] = R_RESERV.subn('', t)
    open(p, "w", encoding="utf-8").write(t)
    print(f"{fn:22s} rename={c['rename']} eventos+={c['eventos']} card_nav-={c['card_nav']} "
          f"ifood->card={c['ifood']} reservar-={c['reservar']}")
