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

import json

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


def main() -> int:
    parser = build_auth_parser("Insert a record into a Feishu bitable table")
    parser.add_argument("--app-token", required=True)
    parser.add_argument("--table-id", required=True)
    parser.add_argument(
        "--fields-json",
        required=True,
        help='JSON object keyed by real field names, e.g. {"文本":"hello","日期":1774541250017}',
    )
    args = parser.parse_args()
    token = tenant_access_token(args.app_id, args.app_secret)
    fields = json.loads(args.fields_json)
    payload = api_request(
        "POST",
        f"{API_ROOT}/bitable/v1/apps/{args.app_token}/tables/{args.table_id}/records",
        token=token,
        json={"fields": fields},
    )
    dump(payload)
    return 0


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