#!/usr/bin/env python3
# args_json: {"type":"object","required":["path","line","col","new_name"],"properties":{"path":{"type":"string","description":"Absolute file path"},"line":{"type":"string","description":"Line number (1-indexed)"},"col":{"type":"string","description":"Column number (1-indexed)"},"new_name":{"type":"string","description":"New symbol name"}}}
# ollie:prompt
# ## lsp_rename
#
# Rename symbol across workspace. Applies edits directly.
#
# **Args**: `[path, line, col, new_name]`
#
# ```
# lsp_rename(path="/abs/path.go", line="42", col="10", new_name="NewName")
# ```
#
# **Prefer over manual find-and-replace** — understands scope, interfaces, cross-package references.
# ollie:end

import os
import sys
sys.path.insert(0, os.environ.get('OLLIE_TOOLS_PATH', os.path.dirname(os.path.abspath(__file__))))

from _lib.args import parse_args
from _lib.lsp.client import request

args = parse_args()
filepath = os.path.abspath(args.require("path"))
line = int(args.require("line")) - 1
col = int(args.require("col")) - 1
new_name = args.require("new_name")

result = request("rename", file=filepath, line=line, col=col, new_name=new_name)

if not result:
    print("(rename returned no edits)")
    sys.exit(0)

files_changed = result.get("files_changed", {})
total = result.get("total_edits", 0)

for path, count in files_changed.items():
    print(f"  {path} ({count} edits)")

print(f"\nRenamed to '{new_name}': {total} edits across {len(files_changed)} files.")
