"""Migrate file_cache.py: extract Result-returning _get_mtime_safe helper."""
from __future__ import annotations
from pathlib import Path

p = Path("src/file_cache.py")
content = p.read_text(encoding="utf-8")

# Add Result imports
old_imports = "from typing  import Any, Dict, List, Optional, Tuple"
new_imports = (
    "from typing  import Any, Dict, List, Optional, Tuple\n"
    "\n"
    "from src.result_types import ErrorInfo, ErrorKind, Result"
)
if old_imports not in content:
    print("ERROR: imports not found")
    raise SystemExit(1)
content = content.replace(old_imports, new_imports)

# Replace the try/except in get_cached_tree with helper call
old_block = (
    "  try:\n"
    "   p     = Path(path)\n"
    "   mtime = p.stat().st_mtime if p.exists() else 0.0\n"
    "  except (OSError, ValueError):\n"
    "   mtime = 0.0"
)
new_block = (
    "  mtime_result = _get_mtime_safe(path)\n"
    "  mtime = mtime_result.data  # 0.0 on error (Result.errors has the details)"
)
if old_block not in content:
    print("ERROR: mtime block not found")
    raise SystemExit(1)
content = content.replace(old_block, new_block)

# Add helper after _ast_cache definition, before class ASTParser
helper = '''

def _get_mtime_safe(path: Optional[str]) -> Result[float]:
 """Get file mtime, returning Result[float] with errors on OSError/ValueError.

 The convention requires Result[T] for try/except sites that can fail. Used
 by ASTParser.get_cached_tree to abstract the mtime computation; the caller
 uses `.data` (0.0 fallback) and can inspect `.errors` if needed.
 """
 if path is None:
  return Result(data=0.0)
 try:
  p = Path(path)
  mtime = p.stat().st_mtime if p.exists() else 0.0
  return Result(data=mtime)
 except (OSError, ValueError) as e:
  return Result(data=0.0, errors=[ErrorInfo(
   kind=ErrorKind.INTERNAL,
   message=f"failed to get mtime for {path}: {e}",
   source="file_cache._get_mtime_safe",
   original=e,
  )])
'''

old_class_marker = "\n\nclass ASTParser:"
new_class_marker = helper + "\n\nclass ASTParser:"
if old_class_marker not in content:
    print("ERROR: class marker not found")
    raise SystemExit(1)
content = content.replace(old_class_marker, new_class_marker)

p.write_text(content, encoding="utf-8", newline="")
print("ok")