#!/usr/bin/env python3
from pathlib import Path
import shutil, sys
root=Path(sys.argv[1]) if len(sys.argv)>1 else Path.cwd()
front=root/'frontend'
src=Path(__file__).resolve().parent
(front/'js').mkdir(parents=True, exist_ok=True)
if (src/'hard_preview_contact_v249403.js').is_file():
    shutil.copy2(src/'hard_preview_contact_v249403.js', front/'js/hard_preview_contact_v249403.js')
html=front/'index.html'
s=html.read_text(encoding='utf-8')
s=s.replace('249402-hard-preview-contact-clean','249403-desktop-safe-preview-contact-truth')
s=s.replace('/js/hard_preview_contact_v249402.js?v=249403-desktop-safe-preview-contact-truth','/js/hard_preview_contact_v249403.js?v=249403-desktop-safe-preview-contact-truth')
if 'hard_preview_contact_v249403.js' not in s:
    tag='<script src="/js/hard_preview_contact_v249403.js?v=249403-desktop-safe-preview-contact-truth"></script>\n'
    s=s.replace('</body>', tag+'</body>') if '</body>' in s else s+'\n'+tag
html.write_text(s,encoding='utf-8')
relay=root/'relay_server_clean.py'
r=relay.read_text(encoding='utf-8')
r=r.replace('2.4.9.402-hard-preview-contact-clean','2.4.9.403-desktop-safe-preview-contact-truth')
r=r.replace('249402-hard-preview-contact-clean','249403-desktop-safe-preview-contact-truth')
if 'BEGIN V249403_DESKTOP_SAFE_PREVIEW_CONTACT_TRUTH' not in r:
    r += '''\n# BEGIN V249403_DESKTOP_SAFE_PREVIEW_CONTACT_TRUTH\n# Desktop safe preview and server-truth contacts. No media/message/call/profile deletion.\n@app.get('/api/social/member_lists/v249403')\ndef api_social_member_lists_v249403(room_id: str = Query('ithaki-phone-test'), device_id: str = Query('')):\n    init_social_v249223_db()\n    try: _v249402_dedupe_social(room_id)\n    except Exception: LOG.exception('V249403 cleanup skipped')\n    device_id=str(device_id or '')[:120]\n    friends=_friend_rows_v249223(str(room_id or 'ithaki-phone-test')[:120], device_id) if device_id else []\n    out=[]; seen=set()\n    for p in friends:\n        pid=str((p or {}).get('device_id') or (p or {}).get('id') or '')\n        if not pid or pid in seen or pid==device_id: continue\n        seen.add(pid); out.append(p)\n    return {'ok': True, 'version': 'V249403-desktop-safe-preview-contact-truth', 'source': 'server accepted friends only, no localStorage merge', 'followers': [], 'following': [], 'friends': out, 'counts': {'followers': 0, 'following': 0, 'friends': len(out)}}\n\n@app.get('/api/social/friend/state_v249403')\ndef api_social_friend_state_v249403(room_id: str = Query('ithaki-phone-test'), device_id: str = Query(''), peer_id: str = Query('')):\n    init_social_v249223_db()\n    try: _v249402_dedupe_social(room_id)\n    except Exception: pass\n    return {'ok': True, 'version': 'V249403-desktop-safe-preview-contact-truth', 'room_id': room_id, 'device_id': device_id, 'peer_id': peer_id, 'friends': bool(_are_friends_v249227(room_id, device_id, peer_id)), 'source': 'server truth same as messages/calls'}\n\n@app.get('/api/diagnostics/file_preview_v249403')\ndef api_file_preview_v249403():\n    return {'ok': True, 'version': 'V249403-desktop-safe-preview-contact-truth', 'desktop_safe_preview': True, 'heavy_studio_disabled': True, 'object_url_before_upload': False, 'apk_required_for_this_fix': False}\n\n@app.get('/api/diagnostics/v249403')\ndef api_diagnostics_v249403(room_id: str = Query('ithaki-phone-test'), device_id: str = Query('')):\n    clean=_v249402_dedupe_social(room_id)\n    friends=[]\n    try:\n        friends=_friend_rows_v249223(str(room_id or 'ithaki-phone-test')[:120], str(device_id or '')[:120]) if device_id else []\n    except Exception: pass\n    return {'ok': True, 'version': 'V249403-desktop-safe-preview-contact-truth', 'desktop_safe_preview': True, 'server_only_friends': True, 'local_stale_contacts_ignored': True, 'friends_for_device': len(friends), 'cleanup': clean}\n# END V249403_DESKTOP_SAFE_PREVIEW_CONTACT_TRUTH\n'''
relay.write_text(r,encoding='utf-8')
print('PATCH_SERVER_V249403=OK')
