#!/usr/bin/env python3
"""
Apply Porsche-worthy design to the slides
- Dark background (charcoal/near-black) 
- White text
- Proper typography hierarchy
- Clean spacing and grids
"""

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

def authenticate():
    """Authenticate with Google API"""
    SCOPES = ['https://www.googleapis.com/auth/presentations']
    creds = Credentials.from_authorized_user_file('token.json', SCOPES)
    return creds

def apply_design_system(presentation_id):
    """Apply consistent design system to all slides"""
    
    creds = authenticate()
    service = build('slides', 'v1', credentials=creds)
    
    # Get presentation details
    presentation = service.presentations().get(presentationId=presentation_id).execute()
    
    requests = []
    
    # 1. Apply dark theme to all slides
    for slide in presentation.get('slides', []):
        slide_id = slide.get('objectId')
        
        # Set slide background to dark charcoal
        requests.append({
            'updateSlideProperties': {
                'objectId': slide_id,
                'slideProperties': {
                    'pageBackgroundFill': {
                        'solidFill': {
                            'color': {
                                'rgbColor': {
                                    'red': 0.15,   # Dark charcoal
                                    'green': 0.15,
                                    'blue': 0.15
                                }
                            }
                        }
                    }
                },
                'fields': 'pageBackgroundFill'
            }
        })
        
        # 2. Update text styling for each element
        for element in slide.get('pageElements', []):
            if element.get('shape') and element.get('shape', {}).get('text'):
                element_id = element.get('objectId')
                
                # Determine if this is title or body text
                placeholder = element.get('shape', {}).get('placeholder', {})
                is_title = placeholder.get('type') == 'TITLE'
                
                # Apply white text color
                requests.append({
                    'updateTextStyle': {
                        'objectId': element_id,
                        'style': {
                            'foregroundColor': {
                                'opaqueColor': {
                                    'rgbColor': {
                                        'red': 1.0,    # White text
                                        'green': 1.0,
                                        'blue': 1.0
                                    }
                                }
                            },
                            'fontFamily': 'Arial',  # Clean sans-serif
                            'fontSize': {
                                'magnitude': 44 if is_title else 20,  # Title: 44pt, Body: 20pt
                                'unit': 'PT'
                            },
                            'bold': is_title  # Bold titles only
                        },
                        'fields': 'foregroundColor,fontFamily,fontSize,bold'
                    }
                })
    
    # 3. Fix table formatting on slide 11 (engagement models)
    # Find slide 11 and update table formatting
    slides = presentation.get('slides', [])
    if len(slides) >= 11:
        slide_11 = slides[10]  # 0-indexed, so slide 11 is index 10
        slide_11_id = slide_11.get('objectId')
        
        # Replace table content with properly formatted text
        for element in slide_11.get('pageElements', []):
            if element.get('shape') and element.get('shape', {}).get('text'):
                placeholder = element.get('shape', {}).get('placeholder', {})
                if placeholder.get('type') == 'BODY':
                    element_id = element.get('objectId')
                    
                    # Clear existing text
                    requests.append({
                        'deleteText': {
                            'objectId': element_id,
                            'textRange': {
                                'type': 'ALL'
                            }
                        }
                    })
                    
                    # Insert properly formatted engagement models
                    formatted_text = """PILOT
One campaign, one market. Prove the system on real work.
30 days

PRODUCTION PARTNER  
Ongoing creative production across select markets.
90-day ramp

STRATEGIC INTEGRATION
Full creative system embedded in Porsche's workflow.
6-month build

Recommendation: Start with a pilot. Let the output speak."""
                    
                    requests.append({
                        'insertText': {
                            'objectId': element_id,
                            'text': formatted_text
                        }
                    })
    
    # Execute all updates
    if requests:
        service.presentations().batchUpdate(
            presentationId=presentation_id,
            body={'requests': requests}
        ).execute()
        print("Design system applied successfully")
    else:
        print("No updates needed")

def main():
    presentation_id = "1BpXfnZozs-PdfTFeXX8WkGPcAH3vjLNtzRwrWvIJxPI"
    apply_design_system(presentation_id)
    print(f"Design applied to: https://docs.google.com/presentation/d/{presentation_id}")

if __name__ == '__main__':
    main()