# BEGIN CHANGE: add not_enough_guild_points to shop i18n only
from pathlib import Path
import re

path = Path(r"p:\OT\OTSERVER\pedro\html\portal\src\i18n\messages.ts")
text = path.read_text(encoding="utf-8")

# Type (shop.errors only - first not_enough_points: string in Messages)
# There may be two (shop + characterMarket). Add after shop one only if missing in type block near shop.
if "not_enough_guild_points: string" not in text:
    # Insert after first shop errors not_enough_points in type section
    # Find shop interface: buyOkQueue then errors with not_enough_points
    m = re.search(
        r"(buyOkQueue: string;\n\n    errors: \{[\s\S]*?not_enough_points: string;)\n\n(      buy_failed: string;)",
        text,
    )
    if not m:
        raise SystemExit("shop type errors block not found")
    text = (
        text[: m.end(1)]
        + "\n\n      not_enough_guild_points: string;"
        + text[m.end(1) :]
    )

def insert_after_shop_not_enough(text, locale_buy_failed, guild_line):
    """Insert guild error only where target_account_not_found precedes not_enough_points (shop)."""
    pattern = (
        r"(target_account_not_found: \"[^\"]+\",\n\n"
        r"        not_enough_points: \"[^\"]+\",)\n\n"
        r"(        buy_failed: \"" + re.escape(locale_buy_failed) + r"\",)"
    )
    repl = r"\1\n\n" + guild_line + r"\n\n\2"
    new, n = re.subn(pattern, repl, text, count=1)
    return new, n

# Avoid double-insert
if 'not_enough_guild_points: "Guild Points insuficientes."' not in text:
    text, n1 = insert_after_shop_not_enough(
        text,
        "Falha na compra.",
        '        not_enough_guild_points: "Guild Points insuficientes.",',
    )
    print("pt", n1)
else:
    print("pt already")

if 'not_enough_guild_points: "Not enough Guild Points."' not in text:
    text, n2 = insert_after_shop_not_enough(
        text,
        "Purchase failed.",
        '        not_enough_guild_points: "Not enough Guild Points.",',
    )
    print("en", n2)
else:
    print("en already")

if text.count('not_enough_guild_points: "Guild Points insuficientes."') < 2:
    # ES uses Fallo en la compra - may still need insert; PT already used Guild Points insuficientes
    # Check ES specifically
    if "Fallo en la compra." in text:
        # only insert before ES buy_failed if that block lacks guild key
        idx = text.find('buy_failed: "Fallo en la compra."')
        window = text[max(0, idx - 250) : idx]
        if "not_enough_guild_points" not in window:
            text, n3 = insert_after_shop_not_enough(
                text,
                "Fallo en la compra.",
                '        not_enough_guild_points: "Guild Points insuficientes.",',
            )
            print("es", n3)
        else:
            print("es already in window")
else:
    # might have inserted into both pt and es if same string - check
    print("guild insuficientes count", text.count('not_enough_guild_points: "Guild Points insuficientes."'))

print("total guild keys", text.count("not_enough_guild_points"))
path.write_text(text, encoding="utf-8")
print("ok")
# END CHANGE
