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

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


def main() -> int:
    parser = build_auth_parser("Create Feishu doc")
    parser.add_argument("--title", required=True)
    args = parser.parse_args()
    token = tenant_access_token(args.app_id, args.app_secret)
    payload = api_request(
        "POST",
        f"{API_ROOT}/docx/v1/documents",
        token=token,
        json={"title": args.title},
    )
    document = ((payload.get("data") or {}).get("document") or {}) if isinstance(payload, dict) else {}
    document_id = str(document.get("document_id") or "").strip()
    if document_id:
        document["url_hint"] = f"https://wcntovzrui4f.feishu.cn/docx/{document_id}"
        payload.setdefault("data", {})["document"] = document
    dump(payload)
    return 0


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