from pathlib import Path
import re
import sys

server = Path(sys.argv[1])
relay = server / 'relay_server_clean.py'
text = relay.read_text(encoding='utf-8')
text, count = re.subn(
    r'VERSION\s*=\s*["\'][^"\']+["\']',
    'VERSION  = "2.4.9.397-professional-preview-security-unified-wall"',
    text,
    count=1,
)
if count != 1:
    raise SystemExit(f'VERSION_PATCH_COUNT={count}')

start = '# BEGIN V249397_PROFESSIONAL_PREVIEW_SECURITY_UNIFIED_WALL_DIAGNOSTIC'
if start not in text:
    text += r'''

# BEGIN V249397_PROFESSIONAL_PREVIEW_SECURITY_UNIFIED_WALL_DIAGNOSTIC
@app.get("/api/diagnostics/professional_flow_v249397")
async def diagnostics_professional_flow_v249397():
    return {
        "ok": True,
        "version": "V249397-professional-preview-security-unified-wall",
        "professional_preview_local_open": True,
        "studio_from_preview": True,
        "send_from_preview": True,
        "dm_security_selector_visible": True,
        "security_modes": ["full_e2ee", "standard_aes", "no_encryption"],
        "fcm_message_content_included": False,
        "one_unified_home_wall": True,
        "v411_reactions_in_home_wall": True,
        "professional_wall_duplicate_removed": True,
        "database_deleted": False,
        "media_deleted": False,
    }
# END V249397_PROFESSIONAL_PREVIEW_SECURITY_UNIFIED_WALL_DIAGNOSTIC
'''

relay.write_text(text, encoding='utf-8')
print('SERVER_VERSION_V249397=OK')
print('PROFESSIONAL_FLOW_DIAGNOSTIC_V249397=OK')
