﻿with open("proxy_server.py", "r", encoding="utf-8") as f:
    content = f.read()

import re

target = r"if parsed\.path == '/api/midas-status':"
inject = r"""if parsed.path == '/api/midas-portfolio':
            import subprocess
            bot_path = os.path.join(BASE_DIR, 'midas_bot.py')
            try:
                proc = subprocess.run(['python', bot_path, '--portfolio'], capture_output=True, text=True, timeout=60)
                lines = proc.stdout.strip().split('\n')
                # JSON ciktisi en son satirdadir
                import json
                try:
                    result = json.loads(lines[-1])
                    self._send_json(200, result)
                except Exception as e:
                    self._send_json(500, {'success': False, 'error': f'Bot JSON dondurmedi: {proc.stdout}'})
            except subprocess.TimeoutExpired:
                self._send_json(500, {'success': False, 'error': 'Bot zaman asimi (Midas acilamadi)'})
            except Exception as e:
                self._send_json(500, {'success': False, 'error': str(e)})
            return

        if parsed.path == '/api/midas-status':"""

content = re.sub(target, inject, content)

with open("proxy_server.py", "w", encoding="utf-8") as f:
    f.write(content)
