"""One-off maintenance script to sanitize ``border_color`` usage in ``gestione_layout``.

The script removes incompatible ``border_color='transparent'`` assignments from
widget configuration calls while preserving explicit highlight colors that are
still meaningful for the UI.
"""

import re
from pathlib import Path

# Path default (modifica se serve)
p = Path("./gestione_layout.py")
if not p.exists():
    raise SystemExit(f"File non trovato: {p}")

src = p.read_text(encoding="utf-8")

# 1) Rimuovi i parametri border_color="transparent" nelle chiamate configure(...).
#    Gestiamo i casi ", border_color='transparent'" e "border_color='transparent',"
patterns = [
    re.compile(r""",\s*border_color\s*=\s*["']transparent["']"""),
    re.compile(r"""border_color\s*=\s*["']transparent["']\s*,\s*""")
]
for pat in patterns:
    src = pat.sub("", src)

# 2) Se sono rimaste virgole prima della parentesi di chiusura: ",   )" -> ")"
src = re.sub(r",\s*\)", ")", src)

# 3) Rimuovi anche eventuali border_color=None lasciati da vecchie varianti.
patterns_none = [
    re.compile(r""",\s*border_color\s*=\s*None"""),
    re.compile(r"""border_color\s*=\s*None\s*,\s*""")
]
for pat in patterns_none:
    src = pat.sub("", src)
src = re.sub(r",\s*\)", ")", src)

# 4) Manteniamo eventuali border_color="blue" usati per l'highlight.

# Scrivi backup e nuovo file
bak = p.with_suffix(".py.bak_fix_bc_transparent")
if not bak.exists():
    # Keep a backup copy before overwriting the target file.
    bak.write_text(Path(p).read_text(encoding="utf-8"), encoding="utf-8")

p.write_text(src, encoding="utf-8")
print(f"Patch applicata a {p}. Backup: {bak}")
