from datetime import datetime, timezone, timedelta
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build

hkt = timezone(timedelta(hours=8))

# 航班：ZH8919 深圳→烟台 07:55-11:05
# 需要先确认日期 - 用户之前的出行方案是8月
# 先按8月2日（方案一原定日期）创建，用户可调整
flight_date = datetime(2026, 8, 2).date()

creds = Credentials.from_authorized_user_file('/root/.hermes/google_token.json',
                                               scopes=['https://www.googleapis.com/auth/calendar'])
service = build('calendar', 'v3', credentials=creds)

# 提前2小时到机场
dep_airport = datetime(flight_date.year, flight_date.month, flight_date.day, 5, 55, tzinfo=hkt)
dep_flight = datetime(flight_date.year, flight_date.month, flight_date.day, 7, 55, tzinfo=hkt)
arr_flight = datetime(flight_date.year, flight_date.month, flight_date.day, 11, 5, tzinfo=hkt)

events = [
    {
        'summary': '✈️ ZH8919 深圳宝安T3 → 烟台蓬莱T2',
        'description': '航班：ZH8919\n起飞：07:55 深圳宝安T3\n到达：11:05 烟台蓬莱T2\n航空公司：深圳航空',
        'start': dep_flight,
        'end': arr_flight,
        'location': '深圳宝安国际机场T3',
    },
    {
        'summary': '🚗 出发去深圳宝安机场T3',
        'description': '提前2小时到机场，办理值机',
        'start': dep_airport,
        'end': dep_flight,
        'location': '深圳宝安国际机场T3',
    },
]

for e in events:
    event_body = {
        'summary': e['summary'],
        'description': e['description'],
        'location': e.get('location', ''),
        'start': {'dateTime': e['start'].isoformat(), 'timeZone': 'Asia/Hong_Kong'},
        'end': {'dateTime': e['end'].isoformat(), 'timeZone': 'Asia/Hong_Kong'},
        'reminders': {'useDefault': True},
    }
    result = service.events().insert(calendarId='primary', body=event_body).execute()
    print(f"✅ {e['start'].strftime('%m-%d %H:%M')}-{e['end'].strftime('%H:%M')} | {e['summary']}")

print(f"\n日期：{flight_date}（8月2日）")
print("如日期不对请告诉我调整")
