import sys
import json
from google.oauth2 import service_account
from googleapiclient.discovery import build

def edit_doc(document_id, credentials_path):
    creds = service_account.Credentials.from_service_account_file(
        credentials_path, scopes=['https://www.googleapis.com/auth/documents']
    )
    service = build('docs', 'v1', credentials=creds)
    
    requests = [
        {
            'insertText': {
                'location': {
                    'index': 1,
                },
                'text': 'Trợ lý Zen vừa sửa bài\n'
            }
        }
    ]
    
    result = service.documents().batchUpdate(
        documentId=document_id, body={'requests': requests}).execute()
    return result

if __name__ == "__main__":
    doc_id = "1Ymh60_Qe2yX6TgfznwOHwlphv8YMoz3SscJNg_HZtO0"
    creds_path = "/home/openclaw/.openclaw/workspace/google_service_account.json"
    try:
        res = edit_doc(doc_id, creds_path)
        print("Success:", json.dumps(res, indent=2))
    except Exception as e:
        print("Error:", str(e))
        sys.exit(1)
