#!/usr/bin/env python3

# Dynamic analysis - inject debug hooks into baby.py
import sys

# Read original code
with open("baby.py", "r") as f:
    code = f.read()

# Inject print statements at key validation points
# Look for the g0G0 function which seems to extract characters
# Find and insert debug after the function starts
import re
debug_code = re.sub(
    r'(def g0G0\(GgS_595457\):\s+\w+\s+=)',
    r'\1 (lambda: print(f"DEBUG g0G0: input={GgS_595457}", file=__import__("sys").stderr))() or',
    code
)

# Also hook the final comparison
debug_code = debug_code.replace(
    "if g0G0SQuid(g0go[GOG0sQUId:GOGoSQU1d], goG0SQuId_292841) == g0G0SQuid(G0g0squ1d, goG0SQuId_292841):",
    """print(f"DEBUG FINAL: slice g0go[{GOG0sQUId}:{GOGoSQU1d}]='{g0go[GOG0sQUId:GOGoSQU1d]}' vs expected G0g0squ1d='{G0g0squ1d}'", file=sys.stderr)
    if g0G0SQuid(g0go[GOG0sQUId:GOGoSQU1d], goG0SQuId_292841) == g0G0SQuid(G0g0squ1d, goG0SQuId_292841):"""
)

# Hook earlier comparisons to see expected patterns
debug_code = debug_code.replace(
    "if g0G0SQuid(g0go[G0G0SqUid:g0g0sqUid], gOg0squ1D) == g0G0SQuid(Squ1d, gOg0squ1D):",
    """print(f"DEBUG CHECK1: slice g0go[{G0G0SqUid}:{g0g0sqUid}]='{g0go[G0G0SqUid:g0g0sqUid] if len(g0go) > G0G0SqUid else ''}' vs expected Squ1d='{Squ1d}'", file=sys.stderr)
    if g0G0SQuid(g0go[G0G0SqUid:g0g0sqUid], gOg0squ1D) == g0G0SQuid(Squ1d, gOg0squ1D):"""
)

# Write debug version
with open("baby_trace.py", "w") as f:
    f.write(debug_code)

print("Created baby_trace.py with debug hooks")
print("\nTesting with a long random input to see expected patterns...\n")

import subprocess

# Try with a long input to trigger validation logic
test_input = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"

result = subprocess.run(
    ["python", "baby_trace.py"],
    input=test_input,
    capture_output=True,
    text=True,
    timeout=2
)

print("STDOUT:", result.stdout[:500] if len(result.stdout) > 500 else result.stdout)
print("\n" + "="*80)
print("STDERR (Debug output):")
print(result.stderr)
