"""Clientes tras aplanar: hero Google con la nota 4,3; +14 y 3 sin responder como medias tiles; párrafo y Responder en tile ancha."""
import sys, re, pathlib
sys.path.insert(0, str(pathlib.Path(__file__).parent)); from bentofy import find_close, renumber
p = pathlib.Path(sys.argv[1]); h = p.read_text(encoding="utf-8")
def rng(h, open_tag):
    i = h.find(open_tag); assert i >= 0, open_tag; return i, find_close(h, i)
# hero: cabecera como tile__head; sacar párrafo + botón; meter la cifra 4,3
hs, he = rng(h, '<div class="tile tile--hero" style="--i:0;">'); hero = h[hs:he]
hero = hero.replace('<div class="tile__kv"><p class="tile__label">Google</p>', '<div class="tile__head"><p class="tile__label">Google</p>', 1)
m = re.search(r'<p class="tile__note tile__note--light">Una de las tres.*?</p><a href="#" class="btn btn--secondary">Responder</a>', hero, re.S); assert m
rest = m.group(0); hero = hero.replace(rest, "", 1)
hero = hero[:-6] + '<div class="tile__figure"><span>4,3</span><span class="tile__unit">nota media en Google</span></div></div>'
h = h[:hs] + hero + h[he:]
# quitar la tile de 4,3
s, e = rng(h, '<div class="tile" style="--i:1;">'); h = h[:s] + h[e:]
# +14 con rótulo
h = h.replace('<div class="tile" style="--i:2;"><div class="tile__figure"><span>+14</span>', '<div class="tile" style="--i:2;"><div class="tile__head"><p class="tile__label">Este mes</p></div><div class="tile__figure"><span>+14</span>', 1)
# 3 sin responder pasa a media tile con rótulo; detrás, tile ancha con párrafo y botón
s, e = rng(h, '<div class="tile tile--wide" style="--i:3;">'); t3 = h[s:e]
t3 = t3.replace('<div class="tile tile--wide" style="--i:3;">', '<div class="tile" style="--i:3;"><div class="tile__head"><p class="tile__label">Reseñas</p></div>', 1)
wide = '<div class="tile tile--wide" style="--i:4;">' + rest.replace('class="tile__note tile__note--light"', 'class="tile__body"', 1).replace('<a href="#" class="btn btn--secondary">', '<a href="#" class="btn btn--secondary" style="flex-grow:0;">', 1) + '</div>'
h = h[:s] + t3 + wide + h[e:]
h = renumber(h); p.write_text(h, encoding="utf-8")
print("clientes:", re.findall(r'<(?:a|div) class="(tile[^"]*)"[^>]*style="--i:(\d)', h), "· rótulos:", re.findall(r'tile__label">([^<]*)', h))
