#!/usr/bin/env python3
"""Style the Brandwatch pitch deck with CE branding."""

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

PRESENTATION_ID = "1MRxdQp2Njr-Z5s45c_kV3oC4X-devR4_u5_LpLQ6MpA"

with open('/root/.config/gws/credentials.json') as f:
    creds_data = json.load(f)

creds = Credentials(
    token=None,
    refresh_token=creds_data['refresh_token'],
    token_uri=creds_data['token_uri'],
    client_id=creds_data['client_id'],
    client_secret=creds_data['client_secret']
)

service = build('slides', 'v1', credentials=creds)
pres = service.presentations().get(presentationId=PRESENTATION_ID).execute()

requests = []

# CE colors
CE_RED = {'red': 0.8, 'green': 0, 'blue': 0}  # #cc0000
CE_BLACK = {'red': 0.102, 'green': 0.102, 'blue': 0.102}  # #1a1a1a
CE_GREY = {'red': 0.4, 'green': 0.4, 'blue': 0.4}  # #666
CE_WHITE = {'red': 1, 'green': 1, 'blue': 1}

# Set all slide backgrounds to white
for slide in pres['slides']:
    requests.append({
        'updatePageProperties': {
            'objectId': slide['objectId'],
            'pageProperties': {
                'pageBackgroundFill': {
                    'solidFill': {
                        'color': {'rgbColor': CE_WHITE}
                    }
                }
            },
            'fields': 'pageBackgroundFill'
        }
    })

# Style text elements
for slide_idx, slide in enumerate(pres['slides']):
    for element in slide.get('pageElements', []):
        if 'shape' not in element:
            continue
        shape = element['shape']
        if 'placeholder' not in shape:
            continue
        
        ph_type = shape['placeholder'].get('type', '')
        obj_id = element['objectId']
        
        # Check if text exists
        text_content = shape.get('text', {})
        text_elements = text_content.get('textElements', [])
        
        has_text = False
        for te in text_elements:
            if 'textRun' in te:
                has_text = True
                break
        
        if not has_text:
            continue
        
        if ph_type in ('CENTERED_TITLE', 'TITLE'):
            # Title styling - CE black, clean
            requests.append({
                'updateTextStyle': {
                    'objectId': obj_id,
                    'style': {
                        'foregroundColor': {'opaqueColor': {'rgbColor': CE_BLACK}},
                        'fontSize': {'magnitude': 36, 'unit': 'PT'},
                        'bold': False,
                    },
                    'textRange': {'type': 'ALL'},
                    'fields': 'foregroundColor,fontSize,bold'
                }
            })
        elif ph_type == 'SUBTITLE':
            requests.append({
                'updateTextStyle': {
                    'objectId': obj_id,
                    'style': {
                        'foregroundColor': {'opaqueColor': {'rgbColor': CE_GREY}},
                        'fontSize': {'magnitude': 18, 'unit': 'PT'},
                        'bold': False,
                    },
                    'textRange': {'type': 'ALL'},
                    'fields': 'foregroundColor,fontSize,bold'
                }
            })
        elif ph_type == 'BODY':
            requests.append({
                'updateTextStyle': {
                    'objectId': obj_id,
                    'style': {
                        'foregroundColor': {'opaqueColor': {'rgbColor': CE_BLACK}},
                        'fontSize': {'magnitude': 14, 'unit': 'PT'},
                        'bold': False,
                    },
                    'textRange': {'type': 'ALL'},
                    'fields': 'foregroundColor,fontSize,bold'
                }
            })

if requests:
    service.presentations().batchUpdate(
        presentationId=PRESENTATION_ID,
        body={'requests': requests}
    ).execute()
    print(f"Applied {len(requests)} style updates")

print(f"Deck: https://docs.google.com/presentation/d/{PRESENTATION_ID}/edit")
