File size: 13,842 Bytes
8e4018d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import requests
import json
import os
from typing import Dict, List, Any, Optional, Tuple
from datetime import datetime, timedelta
import pytz

from utils.logging import setup_logger
from utils.error_handling import handle_exceptions, IntegrationError
from utils.storage import load_data, save_data

# Initialize logger
logger = setup_logger(__name__)

class GoogleCalendarIntegration:
    """Google Calendar API integration for event synchronization"""
    
    def __init__(self, credentials: Optional[Dict[str, Any]] = None):
        """Initialize Google Calendar integration
        
        Args:
            credentials: Google API credentials (optional)
        """
        self.base_url = "https://www.googleapis.com/calendar/v3"
        self.credentials = credentials
        self.token = credentials.get("access_token") if credentials else None
        self.refresh_token = credentials.get("refresh_token") if credentials else None
        self.token_expiry = credentials.get("expiry") if credentials else None
        self.headers = {}
        
        if self.token:
            self.headers["Authorization"] = f"Bearer {self.token}"
    
    @handle_exceptions
    def set_credentials(self, credentials: Dict[str, Any]) -> None:
        """Set Google API credentials
        
        Args:
            credentials: Google API credentials
        """
        self.credentials = credentials
        self.token = credentials.get("access_token")
        self.refresh_token = credentials.get("refresh_token")
        self.token_expiry = credentials.get("expiry")
        
        if self.token:
            self.headers["Authorization"] = f"Bearer {self.token}"
    
    @handle_exceptions
    def refresh_access_token(self) -> bool:
        """Refresh the access token using the refresh token
        
        Returns:
            True if token refresh is successful, False otherwise
        """
        # In a real implementation, this would use the refresh token to get a new access token
        # For demo purposes, we'll just return False
        logger.warning("Token refresh not implemented in demo")
        return False
    
    @handle_exceptions
    def test_connection(self) -> bool:
        """Test Google Calendar API connection
        
        Returns:
            True if connection is successful, False otherwise
        """
        try:
            # Check if token is expired
            if self.token_expiry and datetime.fromisoformat(self.token_expiry) < datetime.now():
                # Try to refresh token
                if not self.refresh_access_token():
                    return False
            
            response = requests.get(
                f"{self.base_url}/users/me/calendarList",
                headers=self.headers
            )
            return response.status_code == 200
        except Exception as e:
            logger.error(f"Google Calendar connection test failed: {str(e)}")
            return False
    
    @handle_exceptions
    def get_calendars(self) -> List[Dict[str, Any]]:
        """Get user calendars
        
        Returns:
            List of calendars
        """
        response = requests.get(
            f"{self.base_url}/users/me/calendarList",
            headers=self.headers
        )
        
        if response.status_code != 200:
            raise IntegrationError(f"Failed to get calendars: {response.text}")
        
        calendars = response.json().get("items", [])
        return [{
            "id": calendar.get("id"),
            "name": calendar.get("summary"),
            "description": calendar.get("description"),
            "timezone": calendar.get("timeZone"),
            "primary": calendar.get("primary", False),
            "color": calendar.get("backgroundColor")
        } for calendar in calendars]
    
    @handle_exceptions
    def get_events(self, calendar_id: str, start_time: datetime = None, 
                  end_time: datetime = None, max_results: int = 100) -> List[Dict[str, Any]]:
        """Get calendar events
        
        Args:
            calendar_id: Calendar ID
            start_time: Start time for events (optional)
            end_time: End time for events (optional)
            max_results: Maximum number of events to return
            
        Returns:
            List of events
        """
        params = {
            "maxResults": max_results,
            "singleEvents": True,
            "orderBy": "startTime"
        }
        
        if start_time:
            params["timeMin"] = start_time.isoformat() + "Z"  # RFC3339 timestamp
        
        if end_time:
            params["timeMax"] = end_time.isoformat() + "Z"  # RFC3339 timestamp
        
        response = requests.get(
            f"{self.base_url}/calendars/{calendar_id}/events",
            headers=self.headers,
            params=params
        )
        
        if response.status_code != 200:
            raise IntegrationError(f"Failed to get events: {response.text}")
        
        events = response.json().get("items", [])
        return [self._format_event(event) for event in events]
    
    @handle_exceptions
    def create_event(self, calendar_id: str, summary: str, start: Dict[str, Any], 
                    end: Dict[str, Any], description: str = None, location: str = None, 
                    attendees: List[Dict[str, str]] = None) -> Dict[str, Any]:
        """Create a new calendar event
        
        Args:
            calendar_id: Calendar ID
            summary: Event summary/title
            start: Event start time (dict with 'dateTime' or 'date')
            end: Event end time (dict with 'dateTime' or 'date')
            description: Event description (optional)
            location: Event location (optional)
            attendees: List of attendees (optional)
            
        Returns:
            Created event data
        """
        data = {
            "summary": summary,
            "start": start,
            "end": end
        }
        
        if description:
            data["description"] = description
        
        if location:
            data["location"] = location
        
        if attendees:
            data["attendees"] = attendees
        
        response = requests.post(
            f"{self.base_url}/calendars/{calendar_id}/events",
            headers=self.headers,
            json=data
        )
        
        if response.status_code not in [200, 201]:
            raise IntegrationError(f"Failed to create event: {response.text}")
        
        event = response.json()
        return self._format_event(event)
    
    @handle_exceptions
    def update_event(self, calendar_id: str, event_id: str, summary: str = None, 
                    start: Dict[str, Any] = None, end: Dict[str, Any] = None, 
                    description: str = None, location: str = None) -> Dict[str, Any]:
        """Update an existing calendar event
        
        Args:
            calendar_id: Calendar ID
            event_id: Event ID
            summary: Event summary/title (optional)
            start: Event start time (optional)
            end: Event end time (optional)
            description: Event description (optional)
            location: Event location (optional)
            
        Returns:
            Updated event data
        """
        data = {}
        
        if summary is not None:
            data["summary"] = summary
        
        if start is not None:
            data["start"] = start
        
        if end is not None:
            data["end"] = end
        
        if description is not None:
            data["description"] = description
        
        if location is not None:
            data["location"] = location
        
        response = requests.patch(
            f"{self.base_url}/calendars/{calendar_id}/events/{event_id}",
            headers=self.headers,
            json=data
        )
        
        if response.status_code != 200:
            raise IntegrationError(f"Failed to update event: {response.text}")
        
        event = response.json()
        return self._format_event(event)
    
    @handle_exceptions
    def delete_event(self, calendar_id: str, event_id: str) -> bool:
        """Delete a calendar event
        
        Args:
            calendar_id: Calendar ID
            event_id: Event ID
            
        Returns:
            True if deletion is successful
        """
        response = requests.delete(
            f"{self.base_url}/calendars/{calendar_id}/events/{event_id}",
            headers=self.headers
        )
        
        if response.status_code not in [200, 204]:
            raise IntegrationError(f"Failed to delete event: {response.text}")
        
        return True
    
    @handle_exceptions
    def convert_events_to_tasks(self, calendar_id: str, days: int = 7) -> List[Dict[str, Any]]:
        """Convert Google Calendar events to MONA tasks
        
        Args:
            calendar_id: Calendar ID
            days: Number of days to look ahead
            
        Returns:
            List of tasks
        """
        # Get events for the next 'days' days
        start_time = datetime.now(pytz.UTC)
        end_time = start_time + timedelta(days=days)
        
        events = self.get_events(calendar_id, start_time, end_time)
        
        tasks = []
        for event in events:
            # Skip all-day events
            if "date" in event.get("start", {}):
                continue
            
            # Convert event to task format
            start_time = datetime.fromisoformat(event["start"].get("dateTime", "").replace("Z", "+00:00"))
            
            task = {
                "id": f"gcal-event-{event['id']}",
                "title": event["summary"],
                "description": event.get("description", ""),
                "status": "todo",
                "priority": "medium",  # Default priority
                "created_at": event.get("created", datetime.now().isoformat()),
                "updated_at": event.get("updated", datetime.now().isoformat()),
                "due_date": start_time.isoformat(),
                "source": "google_calendar",
                "source_id": event["id"],
                "source_url": event.get("htmlLink", ""),
                "metadata": {
                    "calendar_id": calendar_id,
                    "location": event.get("location", ""),
                    "start": event["start"],
                    "end": event["end"],
                    "attendees": event.get("attendees", [])
                }
            }
            
            tasks.append(task)
        
        return tasks
    
    @handle_exceptions
    def sync_tasks_to_events(self, tasks: List[Dict[str, Any]], calendar_id: str) -> List[Dict[str, Any]]:
        """Sync MONA tasks to Google Calendar events
        
        Args:
            tasks: List of tasks to sync
            calendar_id: Calendar ID
            
        Returns:
            List of synced tasks with updated metadata
        """
        synced_tasks = []
        
        for task in tasks:
            # Skip tasks that are already synced with Google Calendar
            if task.get("source") == "google_calendar":
                synced_tasks.append(task)
                continue
            
            # Skip tasks without due dates
            if not task.get("due_date"):
                synced_tasks.append(task)
                continue
            
            # Create event from task
            due_date = datetime.fromisoformat(task["due_date"].replace("Z", "+00:00"))
            end_date = due_date + timedelta(hours=1)  # Default 1-hour event
            
            event_data = {
                "summary": task["title"],
                "description": task.get("description", ""),
                "start": {
                    "dateTime": due_date.isoformat(),
                    "timeZone": "UTC"
                },
                "end": {
                    "dateTime": end_date.isoformat(),
                    "timeZone": "UTC"
                }
            }
            
            # Create event
            event = self.create_event(
                calendar_id=calendar_id,
                summary=event_data["summary"],
                start=event_data["start"],
                end=event_data["end"],
                description=event_data["description"]
            )
            
            # Update task with Google Calendar metadata
            task.update({
                "source": "google_calendar",
                "source_id": event["id"],
                "source_url": event.get("htmlLink", ""),
                "metadata": {
                    "calendar_id": calendar_id,
                    "start": event["start"],
                    "end": event["end"]
                }
            })
            
            synced_tasks.append(task)
        
        return synced_tasks
    
    def _format_event(self, event: Dict[str, Any]) -> Dict[str, Any]:
        """Format event data for consistent output
        
        Args:
            event: Raw event data from API
            
        Returns:
            Formatted event data
        """
        return {
            "id": event.get("id"),
            "summary": event.get("summary"),
            "description": event.get("description"),
            "location": event.get("location"),
            "start": event.get("start"),
            "end": event.get("end"),
            "created": event.get("created"),
            "updated": event.get("updated"),
            "status": event.get("status"),
            "htmlLink": event.get("htmlLink"),
            "attendees": event.get("attendees", []),
            "organizer": event.get("organizer")
        }