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

import requests

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


def main() -> int:
    parser = build_auth_parser("List Feishu calendar events")
    parser.add_argument("--calendar-id", required=True)
    parser.add_argument("--page-size", type=int, default=50)
    parser.add_argument("--page-token", default="")
    parser.add_argument("--start", default="")
    parser.add_argument("--end", default="")
    args = parser.parse_args()
    token = tenant_access_token(args.app_id, args.app_secret)
    params = {"page_size": max(1, int(args.page_size or 50))}
    if args.page_token:
        params["page_token"] = args.page_token
    if args.start:
        params["start_time"] = args.start
    if args.end:
        params["end_time"] = args.end
    payload = api_request(
        "GET",
        f"{API_ROOT}/calendar/v4/calendars/{args.calendar_id}/events",
        token=token,
        params=params,
    )
    dump(payload)
    return 0


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