#!/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("Append plain text paragraph to a Feishu doc")
    parser.add_argument("--document-id", required=True)
    parser.add_argument("--text", required=True)
    parser.add_argument("--parent-block-id", default="")
    args = parser.parse_args()
    token = tenant_access_token(args.app_id, args.app_secret)
    parent_block_id = args.parent_block_id or args.document_id
    payload = {
        "children": [
            {
                "block_type": 2,
                "text": {
                    "elements": [
                        {
                            "text_run": {
                                "content": args.text,
                            }
                        }
                    ]
                },
            }
        ]
    }
    result = api_request(
        "POST",
        f"{API_ROOT}/docx/v1/documents/{args.document_id}/blocks/{parent_block_id}/children",
        token=token,
        json=payload,
    )
    dump(result)
    return 0


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