#!/usr/bin/env python3
import re
import sys

with open("src/api/server.rs", "r") as f:
    content = f.read()

# Read new handlers
with open("new_handlers.txt", "r") as f:
    new_handlers = f.read()

# Pattern: closing brace of n8n_search, blank line, start of hybrid_search
# Use exact newlines
pattern = r"\}\n\nasync fn hybrid_search\("
replacement = "}\n\n" + new_handlers + "\n\nasync fn hybrid_search("

new_content = re.sub(pattern, replacement, content, count=1)

if new_content == content:
    print("Pattern not found")
    sys.exit(1)

with open("src/api/server.rs", "w") as f:
    f.write(new_content)

print("Inserted BM25 handlers")
