#!/usr/bin/env python3
from google.oauth2 import service_account
from googleapiclient.discovery import build

SERVICE_ACCOUNT_FILE = 'google_service_account.json'
SCOPES = ['https://www.googleapis.com/auth/documents']

url = 'https://docs.google.com/document/d/18Z7qIqBq8lV7vcSa7F943CwId3IhP8vrWgXpzUVa7UI/edit'
doc_id = url.split('/d/')[1].split('/')[0]

creds = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
docs_service = build('docs', 'v1', credentials=creds)

with open('edited_10_pages.txt', 'r', encoding='utf-8') as f:
    edited_text = f.read()

# Prepend the edited text
header = "=== BẢN ĐÃ HIỆU ĐÍNH (10 TRANG ĐẦU) ===\n\n"
footer = "\n\n=== HẾT BẢN HIỆU ĐÍNH ===\n\n"

requests = [
    {
        'insertText': {
            'location': {
                'index': 1,
            },
            'text': header + edited_text + footer
        }
    }
]

docs_service.documents().batchUpdate(documentId=doc_id, body={'requests': requests}).execute()
print("Success!")