#!/usr/bin/env python3
import json
from google.oauth2.credentials import Credentials
from google.auth.transport.requests import Request
from googleapiclient.discovery import build

PRESENTATION_ID = '1lcwPrNeHpwgTWOebzbrptBSYCam1dlsX35ItXT89rj8'
SLIDE_ID = 'g3cad1b3bf36_0_0'
TOKEN_PATH = '/root/.openclaw/workspace/google-auth/token.json'
CREDS_PATH = '/root/.openclaw/workspace/google-auth/credentials.json'

# Auth - merge client info into token
with open(CREDS_PATH) as f:
    client_info = json.load(f)['installed']
with open(TOKEN_PATH) as f:
    token_data = json.load(f)
token_data['client_id'] = client_info['client_id']
token_data['client_secret'] = client_info['client_secret']
creds = Credentials.from_authorized_user_info(token_data)
if creds.expired and creds.refresh_token:
    creds.refresh(Request())
    with open(TOKEN_PATH, 'w') as f:
        f.write(creds.to_json())

service = build('slides', 'v1', credentials=creds)

# Get existing elements to delete
pres = service.presentations().get(presentationId=PRESENTATION_ID).execute()
slide = next(s for s in pres['slides'] if s['objectId'] == SLIDE_ID)
existing_ids = [e['objectId'] for e in slide.get('pageElements', [])]

requests = []

# Delete existing elements
for eid in existing_ids:
    requests.append({'deleteObject': {'objectId': eid}})

# White background
requests.append({
    'updatePageProperties': {
        'objectId': SLIDE_ID,
        'pageProperties': {
            'pageBackgroundFill': {
                'solidFill': {'color': {'rgbColor': {'red': 1, 'green': 1, 'blue': 1}}}
            }
        },
        'fields': 'pageBackgroundFill'
    }
})

EMU = 914400  # 1 inch
SLIDE_W = 9144000
SLIDE_H = 5143500

# Headline textbox
TITLE_ID = 'ce_slide1_title'
requests.append({
    'createShape': {
        'objectId': TITLE_ID,
        'shapeType': 'TEXT_BOX',
        'elementProperties': {
            'pageObjectId': SLIDE_ID,
            'size': {'width': {'magnitude': 7800000, 'unit': 'EMU'}, 'height': {'magnitude': 900000, 'unit': 'EMU'}},
            'transform': {
                'scaleX': 1, 'scaleY': 1, 'translateX': (SLIDE_W - 7800000) // 2, 'translateY': 1600000,
                'unit': 'EMU'
            }
        }
    }
})
requests.append({
    'insertText': {'objectId': TITLE_ID, 'text': 'The Agency That Runs Itself', 'insertionIndex': 0}
})
requests.append({
    'updateTextStyle': {
        'objectId': TITLE_ID,
        'style': {
            'fontFamily': 'Georgia',
            'fontSize': {'magnitude': 54, 'unit': 'PT'},
            'bold': True,
            'foregroundColor': {'opaqueColor': {'rgbColor': {'red': 15/255, 'green': 23/255, 'blue': 42/255}}}
        },
        'textRange': {'type': 'ALL'},
        'fields': 'fontFamily,fontSize,bold,foregroundColor'
    }
})
requests.append({
    'updateParagraphStyle': {
        'objectId': TITLE_ID,
        'style': {'alignment': 'CENTER'},
        'textRange': {'type': 'ALL'},
        'fields': 'alignment'
    }
})

# Body textbox
BODY_ID = 'ce_slide1_body'
requests.append({
    'createShape': {
        'objectId': BODY_ID,
        'shapeType': 'TEXT_BOX',
        'elementProperties': {
            'pageObjectId': SLIDE_ID,
            'size': {'width': {'magnitude': 7200000, 'unit': 'EMU'}, 'height': {'magnitude': 600000, 'unit': 'EMU'}},
            'transform': {
                'scaleX': 1, 'scaleY': 1, 'translateX': (SLIDE_W - 7200000) // 2, 'translateY': 2700000,
                'unit': 'EMU'
            }
        }
    }
})
requests.append({
    'insertText': {'objectId': BODY_ID, 'text': 'Autonomous systems. Human ambition. No busywork in between.', 'insertionIndex': 0}
})
requests.append({
    'updateTextStyle': {
        'objectId': BODY_ID,
        'style': {
            'fontFamily': 'Inter',
            'fontSize': {'magnitude': 28, 'unit': 'PT'},
            'foregroundColor': {'opaqueColor': {'rgbColor': {'red': 100/255, 'green': 116/255, 'blue': 139/255}}}
        },
        'textRange': {'type': 'ALL'},
        'fields': 'fontFamily,fontSize,foregroundColor'
    }
})
requests.append({
    'updateParagraphStyle': {
        'objectId': BODY_ID,
        'style': {'alignment': 'CENTER'},
        'textRange': {'type': 'ALL'},
        'fields': 'alignment'
    }
})

# Thin red accent line
LINE_ID = 'ce_slide1_accent'
line_w = 1800000
requests.append({
    'createLine': {
        'objectId': LINE_ID,
        'lineCategory': 'STRAIGHT',
        'elementProperties': {
            'pageObjectId': SLIDE_ID,
            'size': {'width': {'magnitude': line_w, 'unit': 'EMU'}, 'height': {'magnitude': 0, 'unit': 'EMU'}},
            'transform': {
                'scaleX': 1, 'scaleY': 1, 'translateX': (SLIDE_W - line_w) // 2, 'translateY': 2500000,
                'unit': 'EMU'
            }
        }
    }
})
requests.append({
    'updateLineProperties': {
        'objectId': LINE_ID,
        'lineProperties': {
            'lineFill': {
                'solidFill': {'color': {'rgbColor': {'red': 204/255, 'green': 0, 'blue': 0}}}
            },
            'weight': {'magnitude': 2, 'unit': 'PT'}
        },
        'fields': 'lineFill,weight'
    }
})

result = service.presentations().batchUpdate(presentationId=PRESENTATION_ID, body={'requests': requests}).execute()
print(f"Done. {len(result.get('replies', []))} operations applied.")
