#!/usr/bin/env python3
"""Add Google Slides API scope to existing OAuth token."""

import os
import json
from google_auth_oauthlib.flow import InstalledAppFlow

SCOPES = [
    "https://www.googleapis.com/auth/gmail.readonly",
    "https://www.googleapis.com/auth/gmail.send",
    "https://www.googleapis.com/auth/gmail.modify",
    "https://www.googleapis.com/auth/presentations",  # Full Slides access
    "https://www.googleapis.com/auth/drive.file",     # Access to files created/opened by app
]

CREDENTIALS_PATH = os.path.expanduser("~/.clawdbot/skills/gmail/credentials.json")
TOKEN_PATH = os.path.expanduser("~/.clawdbot/skills/gmail/tokens/cos.json")

def main():
    print("Starting OAuth flow with Slides scope...")
    print(f"Scopes: {SCOPES}")
    
    flow = InstalledAppFlow.from_client_secrets_file(CREDENTIALS_PATH, SCOPES)
    
    # Run console flow - prints URL for user to visit
    creds = flow.run_local_server(port=18999, open_browser=False, authorization_prompt_message="Please visit this URL to authorize:\n{url}", success_message="Authorization complete! You can close this tab.")
    
    # Save the new token
    token_data = {
        "token": creds.token,
        "refresh_token": creds.refresh_token,
        "token_uri": creds.token_uri,
        "client_id": creds.client_id,
        "client_secret": creds.client_secret,
        "scopes": list(creds.scopes),
        "universe_domain": "googleapis.com",
        "account": "",
        "expiry": creds.expiry.isoformat() + "Z" if creds.expiry else None
    }
    
    with open(TOKEN_PATH, "w") as f:
        json.dump(token_data, f)
    
    print(f"\n✅ Token saved to {TOKEN_PATH}")
    print(f"Scopes granted: {list(creds.scopes)}")

if __name__ == "__main__":
    main()
