#!/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("Send a Feishu interactive card message to a chat")
    parser.add_argument("--chat-id", required=True)
    parser.add_argument("--title", required=True)
    parser.add_argument("--markdown", required=True, help="Card body markdown")
    args = parser.parse_args()
    token = tenant_access_token(args.app_id, args.app_secret)
    card = {
        "config": {"wide_screen_mode": True},
        "header": {
            "title": {
                "tag": "plain_text",
                "content": args.title,
            }
        },
        "elements": [
            {
                "tag": "div",
                "text": {
                    "tag": "lark_md",
                    "content": args.markdown,
                },
            }
        ],
    }
    payload = api_request(
        "POST",
        f"{API_ROOT}/im/v1/messages?receive_id_type=chat_id",
        token=token,
        json={
            "receive_id": args.chat_id,
            "msg_type": "interactive",
            "content": json.dumps(card, ensure_ascii=False),
        },
    )
    dump(payload)
    return 0


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