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

import argparse
import json

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


def main() -> int:
    parser = build_auth_parser("Write values into a Feishu spreadsheet range")
    parser.add_argument("--spreadsheet-token", required=True)
    parser.add_argument("--range", required=True, help="Use sheet_id style ranges, e.g. 026ff1!A1:B2")
    parser.add_argument(
        "--values-json",
        required=True,
        help='2D array JSON, e.g. [["A1","B1"],["A2","B2"]]',
    )
    args = parser.parse_args()
    token = tenant_access_token(args.app_id, args.app_secret)
    values = json.loads(args.values_json)
    payload = api_request(
        "PUT",
        f"{API_ROOT}/sheets/v2/spreadsheets/{args.spreadsheet_token}/values",
        token=token,
        json={"valueRange": {"range": args.range, "values": values}},
    )
    dump(payload)
    return 0


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