File size: 3,063 Bytes
0819f43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from datetime import datetime, timedelta
from icalendar import Calendar, Event
from pathlib import Path
import pytz


async def create_calendar_event(
    title: str,
    start: str,
    end: str,
    description: str = '',
    location: str = ''
) -> dict:
    """
    Create ICS calendar event
    
    Args:
        title: Event title
        start: Start datetime (ISO format or natural language)
        end: End datetime (ISO format or natural language)
        description: Event description
        location: Event location
        
    Returns:
        Dict with ICS file path
    """
    try:
        # Parse dates
        start_dt = parse_datetime(start)
        end_dt = parse_datetime(end)
        
        # Create calendar
        cal = Calendar()
        cal.add('prodid', '-//LifeAdmin AI//lifeadmin.ai//')
        cal.add('version', '2.0')
        
        # Create event
        event = Event()
        event.add('summary', title)
        event.add('dtstart', start_dt)
        event.add('dtend', end_dt)
        event.add('description', description)
        if location:
            event.add('location', location)
        event.add('dtstamp', datetime.now(pytz.UTC))
        
        # Generate unique ID
        import uuid
        event.add('uid', str(uuid.uuid4()))
        
        cal.add_component(event)
        
        # Save to file
        output_path = f"data/outputs/event_{datetime.now().strftime('%Y%m%d_%H%M%S')}.ics"
        Path(output_path).parent.mkdir(parents=True, exist_ok=True)
        
        with open(output_path, 'wb') as f:
            f.write(cal.to_ical())
        
        return {
            'success': True,
            'output_path': output_path,
            'event': {
                'title': title,
                'start': start_dt.isoformat(),
                'end': end_dt.isoformat(),
                'description': description
            }
        }
        
    except Exception as e:
        return {'error': str(e), 'success': False}


def parse_datetime(dt_string: str) -> datetime:
    """Parse datetime from various formats"""
    try:
        # Try ISO format
        return datetime.fromisoformat(dt_string.replace('Z', '+00:00'))
    except:
        pass
    
    try:
        # Try common formats
        for fmt in ['%Y-%m-%d %H:%M', '%Y-%m-%d', '%m/%d/%Y %H:%M', '%m/%d/%Y']:
            try:
                dt = datetime.strptime(dt_string, fmt)
                return pytz.UTC.localize(dt)
            except:
                continue
    except:
        pass
    
    # Default to now
    return datetime.now(pytz.UTC)


async def create_multiple_events(events: list) -> dict:
    """Create multiple calendar events from a list"""
    results = []
    for event_data in events:
        result = await create_calendar_event(**event_data)
        results.append(result)
    
    successful = sum(1 for r in results if r.get('success'))
    
    return {
        'success': True,
        'total_events': len(events),
        'successful': successful,
        'results': results
    }