#!/usr/bin/env python3
"""
FiBo Deck Redesign — Apply Fibonacci visual language to FiBo pitch deck
Deck ID: 19aLgS9AQaEoofu_ZOCBzcK4lUVfKpeSofOS1Z1WH-Nc

Design changes:
- Blue (#1565C0) → Coral-pink (#E8446D) everywhere
- Cover + Close slides: coral-pink solid background, white text
- Remove/hide the blue border frame element on slide 1
- Table alt-row: light blue tint → light pink tint
- All text on gradient backgrounds → white

Content is FROZEN. No text changes.
"""

import json, time
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build as gapi_build
from google.auth.transport.requests import Request

DECK_ID = '19aLgS9AQaEoofu_ZOCBzcK4lUVfKpeSofOS1Z1WH-Nc'

# ── Auth ──────────────────────────────────────────────────────────────────────
with open('/root/.openclaw/workspace/google-auth/token.json') as f:
    d = json.load(f)

creds = Credentials(
    token=d.get('token') or d.get('access_token'),
    refresh_token=d.get('refresh_token'),
    token_uri='https://oauth2.googleapis.com/token',
    client_id=d.get('client_id'),
    client_secret=d.get('client_secret'),
    scopes=['https://www.googleapis.com/auth/presentations',
            'https://www.googleapis.com/auth/drive']
)
if creds.expired or not creds.valid:
    creds.refresh(Request())

svc = gapi_build('slides', 'v1', credentials=creds)

# ── Design tokens ─────────────────────────────────────────────────────────────
# NEW: Coral-pink accent (Fibonacci brand)
CORAL      = {'red': 0.910, 'green': 0.267, 'blue': 0.427}   # #E8446D — primary
CORAL_DARK = {'red': 0.761, 'green': 0.094, 'blue': 0.357}   # #C2185B — deep magenta
WHITE      = {'red': 1.0,   'green': 1.0,   'blue': 1.0  }
BLACK      = {'red': 0.102, 'green': 0.102, 'blue': 0.102}   # #1A1A1A
DGRAY      = {'red': 0.267, 'green': 0.267, 'blue': 0.267}   # #444444
MGRAY      = {'red': 0.533, 'green': 0.533, 'blue': 0.533}   # #888888
LGRAY      = {'red': 0.878, 'green': 0.878, 'blue': 0.878}   # #E0E0E0
PINKLT     = {'red': 0.992, 'green': 0.949, 'blue': 0.957}   # #FDF2F4 — table alt row

def c_rgb(c):  return {'rgbColor': c}
def c_fg(c):   return {'opaqueColor': {'rgbColor': c}}

def set_fill(oid, color):
    """Update a shape's background fill to a solid color."""
    return {
        'updateShapeProperties': {
            'objectId': oid,
            'shapeProperties': {
                'shapeBackgroundFill': {
                    'solidFill': {'color': c_rgb(color)}
                }
            },
            'fields': 'shapeBackgroundFill'
        }
    }

def set_text_color(oid, color):
    """Update all text in a shape to a given color."""
    return {
        'updateTextStyle': {
            'objectId': oid,
            'style': {
                'foregroundColor': c_fg(color)
            },
            'fields': 'foregroundColor',
            'textRange': {'type': 'ALL'}
        }
    }

def set_page_bg(sid, color):
    """Set page background to solid color."""
    return {
        'updatePageProperties': {
            'objectId': sid,
            'pageProperties': {
                'pageBackgroundFill': {
                    'solidFill': {'color': c_rgb(color)}
                }
            },
            'fields': 'pageBackgroundFill'
        }
    }

def delete_object(oid):
    return {'deleteObject': {'objectId': oid}}

# ══════════════════════════════════════════════════════════════════════════════
# SLIDE 1 — Cover
# ══════════════════════════════════════════════════════════════════════════════
def redesign_slide_01():
    R = []
    # Gradient background (solid approximation — API doesn't support gradient pages)
    R.append(set_page_bg('sld_01', CORAL))
    # Remove the blue frame element
    R.append(delete_object('sld_01_fr'))
    # Slide number → white (subtle on gradient)
    R.append(set_text_color('sld_01_sn', WHITE))
    # Title → white bold
    R.append(set_text_color('sld_01_t1', WHITE))
    # Subtitle "תמיד." → white (was blue)
    R.append(set_text_color('sld_01_t2', WHITE))
    # Rule line → white semi-transparent (use PINKLT for contrast, or white)
    R.append(set_fill('sld_01_rule', {'red': 1.0, 'green': 0.8, 'blue': 0.88}))  # soft white-pink
    # Tagline → white
    R.append(set_text_color('sld_01_tag', WHITE))
    # Date → white (light)
    R.append(set_text_color('sld_01_dt', {'red': 0.95, 'green': 0.85, 'blue': 0.90}))
    return R

# ══════════════════════════════════════════════════════════════════════════════
# SLIDE 2 — Problem (bullets)
# ══════════════════════════════════════════════════════════════════════════════
def redesign_slide_02():
    R = []
    R.append(set_fill('sld_02_rule', CORAL))
    R.append(set_fill('sld_02_kr',   PINKLT))    # kicker background → light pink
    R.append(set_text_color('sld_02_kk', CORAL)) # kicker text → coral
    return R

# ══════════════════════════════════════════════════════════════════════════════
# SLIDE 3 — Market (stats)
# ══════════════════════════════════════════════════════════════════════════════
def redesign_slide_03():
    R = []
    R.append(set_fill('sld_03_rule', CORAL))
    # Stats numbers → coral
    for i in range(4):
        R.append(set_text_color(f'sld_03_sn{i}', CORAL))
    return R

# ══════════════════════════════════════════════════════════════════════════════
# SLIDE 4 — Product (bullets)
# ══════════════════════════════════════════════════════════════════════════════
def redesign_slide_04():
    R = []
    R.append(set_fill('sld_04_rule', CORAL))
    R.append(set_fill('sld_04_kr',   PINKLT))
    R.append(set_text_color('sld_04_kk', CORAL))
    return R

# ══════════════════════════════════════════════════════════════════════════════
# SLIDE 5 — How it works (steps)
# ══════════════════════════════════════════════════════════════════════════════
def redesign_slide_05():
    R = []
    R.append(set_fill('sld_05_rule', CORAL))
    # Step circles → coral
    for cid in ['sld_05_c0', 'sld_05_c1', 'sld_05_c2', 'sld_05_c4']:
        R.append(set_fill(cid, CORAL))
    return R

# ══════════════════════════════════════════════════════════════════════════════
# SLIDE 6 — Dashboard (bullets)
# ══════════════════════════════════════════════════════════════════════════════
def redesign_slide_06():
    R = []
    R.append(set_fill('sld_06_rule', CORAL))
    R.append(set_fill('sld_06_kr',   PINKLT))
    R.append(set_text_color('sld_06_kk', CORAL))
    return R

# ══════════════════════════════════════════════════════════════════════════════
# SLIDE 7 — Comparison table
# ══════════════════════════════════════════════════════════════════════════════
def redesign_slide_07():
    R = []
    R.append(set_fill('sld_07_rule', CORAL))
    # Table header background → coral
    R.append(set_fill('sld_07_hbg', CORAL))
    # Table alt rows → light pink
    for rid in ['sld_07_rb0', 'sld_07_rb2', 'sld_07_rb4']:
        R.append(set_fill(rid, PINKLT))
    # FiBo ✓ column → coral
    for i in range(6):
        R.append(set_text_color(f'sld_07_r{i}c0', CORAL))
    return R

# ══════════════════════════════════════════════════════════════════════════════
# SLIDE 8 — Use cases (cards)
# ══════════════════════════════════════════════════════════════════════════════
def redesign_slide_08():
    R = []
    R.append(set_fill('sld_08_rule', CORAL))
    # Card name text → coral accent for visual energy
    for i in range(6):
        R.append(set_text_color(f'sld_08_cn{i}', CORAL))
    return R

# ══════════════════════════════════════════════════════════════════════════════
# SLIDE 9 — Validation (bullets)
# ══════════════════════════════════════════════════════════════════════════════
def redesign_slide_09():
    R = []
    R.append(set_fill('sld_09_rule', CORAL))
    R.append(set_fill('sld_09_kr',   PINKLT))
    R.append(set_text_color('sld_09_kk', CORAL))
    return R

# ══════════════════════════════════════════════════════════════════════════════
# SLIDE 10 — Business model (table)
# ══════════════════════════════════════════════════════════════════════════════
def redesign_slide_10():
    R = []
    R.append(set_fill('sld_10_rule', CORAL))
    # Table header background → coral
    R.append(set_fill('sld_10_hbg', CORAL))
    # Table alt rows → light pink
    for rid in ['sld_10_rb0', 'sld_10_rb2']:
        R.append(set_fill(rid, PINKLT))
    # Pricing column text → coral
    for i in range(3):
        R.append(set_text_color(f'sld_10_r{i}c1', CORAL))
    # Kicker text → coral
    R.append(set_text_color('sld_10_kk', CORAL))
    return R

# ══════════════════════════════════════════════════════════════════════════════
# SLIDE 11 — Team
# ══════════════════════════════════════════════════════════════════════════════
def redesign_slide_11():
    R = []
    R.append(set_fill('sld_11_rule', CORAL))
    # Avatar circles → coral
    for cid in ['sld_11_av0', 'sld_11_av1']:
        R.append(set_fill(cid, CORAL))
    # Role text → coral
    for rid in ['sld_11_rl0', 'sld_11_rl1']:
        R.append(set_text_color(rid, CORAL))
    return R

# ══════════════════════════════════════════════════════════════════════════════
# SLIDE 12 — CTA/Close
# ══════════════════════════════════════════════════════════════════════════════
def redesign_slide_12():
    R = []
    # Gradient background (solid approximation)
    R.append(set_page_bg('sld_12', CORAL))
    # Slide number → white
    R.append(set_text_color('sld_12_sn', WHITE))
    # Title → white
    R.append(set_text_color('sld_12_ttl', WHITE))
    # Rule → soft white-pink
    R.append(set_fill('sld_12_rule', {'red': 1.0, 'green': 0.8, 'blue': 0.88}))
    # Bullet dots → deep magenta (visible on coral bg)
    for i in range(4):
        R.append(set_fill(f'sld_12_dt{i}', CORAL_DARK))
    # Bullet text → white
    for i in range(4):
        R.append(set_text_color(f'sld_12_o{i}', WHITE))
    # Kicker box → deep magenta (dark coral)
    R.append(set_fill('sld_12_kb', CORAL_DARK))
    # Kicker text stays white (already white)
    return R

# ── Main execution ─────────────────────────────────────────────────────────────

SLIDE_FUNCTIONS = {
    'sld_01': ('Cover — gradient bg, white text, remove frame', redesign_slide_01),
    'sld_02': ('Problem — coral rule, coral kicker', redesign_slide_02),
    'sld_03': ('Market — coral rule, coral stat numbers', redesign_slide_03),
    'sld_04': ('Product — coral rule, coral kicker', redesign_slide_04),
    'sld_05': ('How it works — coral rule, coral step circles', redesign_slide_05),
    'sld_06': ('Dashboard — coral rule, coral kicker', redesign_slide_06),
    'sld_07': ('Comparison table — coral header + alt rows + FiBo checkmarks', redesign_slide_07),
    'sld_08': ('Use cases — coral rule, coral card names', redesign_slide_08),
    'sld_09': ('Validation — coral rule, coral kicker', redesign_slide_09),
    'sld_10': ('Business model — coral header + alt rows + pricing text', redesign_slide_10),
    'sld_11': ('Team — coral rule, coral avatars, coral roles', redesign_slide_11),
    'sld_12': ('CTA/Close — gradient bg, white text, dark kicker box', redesign_slide_12),
}

def execute():
    print(f'🎨 FiBo Deck Redesign — Fibonacci Visual Language')
    print(f'   Deck: https://docs.google.com/presentation/d/{DECK_ID}/edit')
    print()

    # Execute slide 1 + 12 first (highest impact)
    priority_order = ['sld_01', 'sld_12', 'sld_02', 'sld_03', 'sld_04', 'sld_05',
                      'sld_06', 'sld_07', 'sld_08', 'sld_09', 'sld_10', 'sld_11']

    changes_log = {}

    for sid in priority_order:
        label, fn = SLIDE_FUNCTIONS[sid]
        requests = fn()
        n = len(requests)

        try:
            svc.presentations().batchUpdate(
                presentationId=DECK_ID,
                body={'requests': requests}
            ).execute()
            print(f'  ✅ {sid} — {label} ({n} changes)')
            changes_log[sid] = {'label': label, 'changes': n, 'status': 'ok'}
        except Exception as e:
            print(f'  ⚠️  {sid} — {label} — ERROR: {e}')
            changes_log[sid] = {'label': label, 'changes': n, 'status': 'error', 'error': str(e)}
        
        time.sleep(0.4)

    print()
    print(f'✅ Redesign complete.')
    print(f'   URL: https://docs.google.com/presentation/d/{DECK_ID}/edit')
    return changes_log

if __name__ == '__main__':
    log = execute()
    total = sum(v['changes'] for v in log.values())
    ok    = sum(1 for v in log.values() if v['status'] == 'ok')
    print(f'\n📊 Summary: {ok}/12 slides updated, {total} total API changes')
