#!/usr/bin/env python3
# description: Rename symbol at position across the workspace. Applies edits directly to files.
# Args: <file> <line> <col> <new_name>

import os
import sys
sys.path.insert(0, os.path.join(os.environ['OLLIE'], 's', os.environ['OLLIE_SESSION_ID'], 't'))

from _lib.lsp.client import request

if len(sys.argv) < 5:
    print("usage: lsp_rename <file> <line> <col> <new_name>")
    sys.exit(1)

filepath = os.path.abspath(sys.argv[1])
line = int(sys.argv[2]) - 1
col = int(sys.argv[3]) - 1
new_name = sys.argv[4]

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.")
