#!/usr/bin/env python3
from __future__ import annotations

import requests

from _common import API_ROOT, api_request, build_auth_parser, dump, normalize_feishu_event_time, tenant_access_token


def main() -> int:
    parser = build_auth_parser("Create Feishu calendar event")
    parser.add_argument("--calendar-id", required=True)
    parser.add_argument("--summary", required=True)
    parser.add_argument("--start", required=True, help="RFC3339 timestamp with timezone offset or unix timestamp seconds")
    parser.add_argument("--end", required=True, help="RFC3339 timestamp with timezone offset or unix timestamp seconds")
    parser.add_argument("--timezone", default="Asia/Shanghai")
    parser.add_argument("--description", default="")
    args = parser.parse_args()
    token = tenant_access_token(args.app_id, args.app_secret)
    payload = {
        "summary": args.summary,
        "description": args.description,
        "start_time": normalize_feishu_event_time(args.start, args.timezone),
        "end_time": normalize_feishu_event_time(args.end, args.timezone),
    }
    result = api_request(
        "POST",
        f"{API_ROOT}/calendar/v4/calendars/{args.calendar_id}/events",
        token=token,
        json=payload,
    )
    dump(result)
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
