#!/usr/bin/env python3
"""Update typography in Google Slides presentation."""

import os
import json
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build

# Presentation ID from URL
PRESENTATION_ID = "1Q47M-yqQdkOyM7macRu021ZthyN4Ob6yWE79PQW7S8w"

# Typography specs
TITLE_FONT = "Noto Sans Hebrew"
TITLE_WEIGHT = 500  # Medium
TITLE_SIZE = 26

BODY_FONT = "Noto Sans Hebrew"
BODY_WEIGHT = 400  # Normal
BODY_SIZE = 14

TEXT_COLOR = {"red": 0.239, "green": 0.220, "blue": 0.961}  # #3d38f5

def get_credentials():
    token_path = os.path.expanduser("~/.clawdbot/skills/gmail/tokens/cos.json")
    with open(token_path) as f:
        token_data = json.load(f)
    return Credentials.from_authorized_user_info(token_data)

def main():
    creds = get_credentials()
    service = build("slides", "v1", credentials=creds)
    
    # Get presentation
    presentation = service.presentations().get(presentationId=PRESENTATION_ID).execute()
    slides = presentation.get("slides", [])
    
    print(f"Found {len(slides)} slides")
    
    requests = []
    
    for slide in slides:
        for element in slide.get("pageElements", []):
            if "shape" not in element:
                continue
            shape = element["shape"]
            if "text" not in shape:
                continue
            
            object_id = element["objectId"]
            text_elements = shape["text"].get("textElements", [])
            
            # Check if this looks like a title (usually first/larger text box)
            placeholder_type = shape.get("placeholder", {}).get("type", "")
            is_title = placeholder_type in ["TITLE", "CENTERED_TITLE", "SUBTITLE"] or "title" in object_id.lower()
            
            font_size = TITLE_SIZE if is_title else BODY_SIZE
            font_weight = TITLE_WEIGHT if is_title else BODY_WEIGHT
            
            # Update all text in this shape
            requests.append({
                "updateTextStyle": {
                    "objectId": object_id,
                    "textRange": {"type": "ALL"},
                    "style": {
                        "fontFamily": TITLE_FONT if is_title else BODY_FONT,
                        "fontSize": {"magnitude": font_size, "unit": "PT"},
                        "foregroundColor": {
                            "opaqueColor": {"rgbColor": TEXT_COLOR}
                        },
                        "bold": font_weight >= 500
                    },
                    "fields": "fontFamily,fontSize,foregroundColor,bold"
                }
            })
            print(f"  {'TITLE' if is_title else 'BODY'}: {object_id}")
    
    if requests:
        print(f"\nApplying {len(requests)} text style updates...")
        service.presentations().batchUpdate(
            presentationId=PRESENTATION_ID,
            body={"requests": requests}
        ).execute()
        print("Done!")
    else:
        print("No text elements found")

if __name__ == "__main__":
    main()
