#!/usr/bin/env python3
"""Create calendar events via Google Calendar API."""
import json, os, urllib.parse, urllib.request

WORKSPACE = os.environ.get('WORKSPACE', '/root/.openclaw/workspace')
TOKEN_FILE = os.path.join(WORKSPACE, 'google-auth/token.json')

# Events: (summary, start, end, description)
# All times in UTC (Lisbon = UTC+0 in March)
EVENTS = [
    {
        "summary": "📧 Reply: Hila Shechter / eToro C-level Zoom intro",
        "start": "2026-03-13T09:00:00Z",
        "end":   "2026-03-13T09:15:00Z",
        "description": "Reply to hilash@etoro.com — she intro'd you to Patricia for C-level Zoom sessions (eToro rebrand). Patricia is waiting.",
    },
    {
        "summary": "📧 Reply: Marina Teixeira Design — Week 1 Progress",
        "start": "2026-03-13T09:15:00Z",
        "end":   "2026-03-13T09:30:00Z",
        "description": "Read Marina's week 1 update (info@mtx-design.com). She's tracking a plan request made Monday. Reply with feedback or confirmation.",
    },
    {
        "summary": "📧 Decision: Lukas Richthammer Google Drive Access",
        "start": "2026-03-13T09:30:00Z",
        "end":   "2026-03-13T09:45:00Z",
        "description": "Lukas Richthammer (lukas.richthammer@gmail.com) requested access to:\n1. CE Prospect List — LinkedIn Contacts\n2. Porsche × CE presentation\nDecide: grant, deny, or share limited version.",
    },
    {
        "summary": "📧 Decision: Polsia — FionaOS billing + CuriousOS",
        "start": "2026-03-13T09:45:00Z",
        "end":   "2026-03-13T10:00:00Z",
        "description": "Two items:\n1. FionaOS app paused — update billing on Polsia to resume, or cancel.\n2. CuriousOS at curiousos.polsia.app — idle 2 weeks. Continue building or shut down?",
    },
    {
        "summary": "📧 Review: Millennium BCP Account Change (Fiona cc'd you)",
        "start": "2026-03-13T10:00:00Z",
        "end":   "2026-03-13T10:10:00Z",
        "description": "Hugo Carvalho (BCP) replied — commission structure unchanged, Frequent Customer plan stays. Fiona asked for English. Quick read to confirm no action needed.",
    },
    {
        "summary": "💰 Check Anthropic API spend — 7 receipts yesterday",
        "start": "2026-03-13T10:10:00Z",
        "end":   "2026-03-13T10:20:00Z",
        "description": "7 Anthropic receipts hit yesterday (multiple accounts). Review console.anthropic.com for total spend and check if any runaway usage.",
    },
    {
        "summary": "📊 Vanguard proxy vote",
        "start": "2026-03-13T10:20:00Z",
        "end":   "2026-03-13T10:30:00Z",
        "description": "Proxy vote from Vanguard Brokerage Services. Cast your vote at proxyvote.com before the deadline.",
    },
]

def refresh_token():
    with open(TOKEN_FILE) as f:
        d = json.load(f)
    data = urllib.parse.urlencode({
        'client_id': d['client_id'],
        'client_secret': d['client_secret'],
        'refresh_token': d['refresh_token'],
        'grant_type': 'refresh_token',
    }).encode()
    req = urllib.request.Request('https://oauth2.googleapis.com/token', data=data)
    resp = urllib.request.urlopen(req)
    return json.loads(resp.read())['access_token']

def create_event(token, event):
    body = {
        "summary": event["summary"],
        "description": event["description"],
        "start": {"dateTime": event["start"], "timeZone": "UTC"},
        "end":   {"dateTime": event["end"],   "timeZone": "UTC"},
        "reminders": {
            "useDefault": False,
            "overrides": [
                {"method": "popup", "minutes": 5},
            ]
        }
    }
    url = "https://www.googleapis.com/calendar/v3/calendars/primary/events"
    data = json.dumps(body).encode()
    req = urllib.request.Request(url, data=data, headers={
        'Authorization': f'Bearer {token}',
        'Content-Type': 'application/json',
    })
    resp = urllib.request.urlopen(req)
    result = json.loads(resp.read())
    return result.get('id'), result.get('htmlLink')

def main():
    token = refresh_token()
    for ev in EVENTS:
        eid, link = create_event(token, ev)
        print(f"✅ {ev['summary'][:60]} → {link}")

if __name__ == '__main__':
    main()
