"""Apply the 10 send_result -> send edits to src/ai_client.py.

This is a one-shot script for Task 1.1. Idempotent: re-running is a no-op
if the rename is already complete.
"""
from __future__ import annotations

import sys
from pathlib import Path

FILE = Path("src/ai_client.py")

EDITS: list[tuple[str, str]] = [
 (
  "  Immediate-Mode DAG / Thread Context:\n   Called by: send_result\n   Calls: _ensure_grok_client",
  "  Immediate-Mode DAG / Thread Context:\n   Called by: send\n   Calls: _ensure_grok_client",
 ),
 (
  "  Immediate-Mode DAG / Thread Context:\n   Called by: send_result\n   Calls: _ensure_minimax_client",
  "  Immediate-Mode DAG / Thread Context:\n   Called by: send\n   Calls: _ensure_minimax_client",
 ),
 (
  "  Immediate-Mode DAG / Thread Context:\n   Called by: send_result\n   Calls: _ensure_qwen_client",
  "  Immediate-Mode DAG / Thread Context:\n   Called by: send\n   Calls: _ensure_qwen_client",
 ),
 (
  "  Immediate-Mode DAG / Thread Context:\n   Called by: send_result\n   Calls: _send_llama_native",
  "  Immediate-Mode DAG / Thread Context:\n   Called by: send\n   Calls: _send_llama_native",
 ),
 (
  "def send_result(\n md_content: str,",
  "def send(\n md_content: str,",
 ),
 (
  "[C: tests/test_ai_client_result.py:test_send_result_public_api_returns_result, tests/test_ai_client_result.py:test_send_result_preserves_errors, tests/test_deprecation_warnings.py:test_send_result_does_not_emit_deprecation]",
  "[C: tests/test_ai_client_result.py:test_send_public_api_returns_result, tests/test_ai_client_result.py:test_send_preserves_errors, tests/test_deprecation_warnings.py:test_send_does_not_emit_deprecation]",
 ),
 (
  'if monitor.enabled: monitor.start_component("ai_client.send_result")',
  'if monitor.enabled: monitor.start_component("ai_client.send")',
 ),
 (
  'source="ai_client.send_result")])',
  'source="ai_client.send")])',
 ),
 (
  'source="ai_client.send_result", original=exc)',
  'source="ai_client.send", original=exc)',
 ),
 (
  'if monitor.enabled: monitor.end_component("ai_client.send_result")',
  'if monitor.enabled: monitor.end_component("ai_client.send")',
 ),
]


def main() -> int:
 with FILE.open("r", encoding="utf-8", newline="") as f:
  content = f.read()
 has_crlf = "\r\n" in content
 nl = "\r\n" if has_crlf else "\n"
 normalized_edits = [
  (old.replace("\n", nl), new.replace("\n", nl)) for old, new in EDITS
 ]
 new_content = content
 applied = 0
 for old, new in normalized_edits:
  if old in new_content:
   new_content = new_content.replace(old, new, 1)
   applied += 1
  else:
   print(f"NOT FOUND: {old[:80]!r}", file=sys.stderr)
 if applied != len(EDITS):
  print(f"Only applied {applied}/{len(EDITS)} edits. ABORTING.", file=sys.stderr)
  return 1
 with FILE.open("w", encoding="utf-8", newline="") as f:
  f.write(new_content)
 remaining = new_content.count("send_result")
 print(f"Applied {applied}/{len(EDITS)} edits. Remaining send_result: {remaining}")
 print(f"Line endings: {'CRLF' if has_crlf else 'LF'}")
 return 0


if __name__ == "__main__":
 raise SystemExit(main())
