# shellcheck shell=sh disable=SC2154,SC2120,SC3043

# Reference: https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/intro

# Section(inner): endpoint, send_json

___x_cmd_feishu_msg___endpoint(){
#    printf "%s" "https://open.feishu.cn/open-apis/im/v1/chats"
   printf "%s" "https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=chat_id"
}

___x_cmd_feishu_msg___send_json(){
    local json="$1"
    [ -n "$json" ] || json="$(___x_cmd_cmds_cat)"
    echo  curl -i "$(___x_cmd_feishu_msg___endpoint )" \
        -H 'Content-Type:application/json' \
        -H "Authorization: Bearer $___X_CMD_FEISHU_ACCESS_TOKEN" \
        -d "$json"
}

# EndSection

___x_cmd_feishu_msg(){
    param:subcmd ___x_cmd_feishu_msg    \
        send   "send msg"   

    param:subcmd:try
    param:run
    hub:error "Subcommand required"
    ___x_cmd_feishu help
}

___x_cmd_feishu_msg_send() {
    param:subcmd ___x_cmd_feishu_msg_send       \
        reply            "reply msg"         

    param:subcmd:try 

    param:dsl <<A
option:
    --receive_id           "According to the value of receive_id_type, fill in the corresponding message recipient id"                  <>
    --content              "Message content, json structure serialized string"                                                          <>
    --msg_type             "message type"                                                                                               <>

A
    param:run

    ___x_cmd_feishu_msg___send_json <<A
{
    "content": "$content",
    "msg_type": "$msg_type",
    "receive_id": "$receive_id"
} 
A
}

___x_cmd_feishu_msg_send_reply() {
    param:dsl <<A
option:
    --message_id           "ID of the message to be replied to"                                                                         <>
    --content              "Message content, json structure serialized string"                                                          <>
    --msg_type             "message type"                                                                                               <>

A
    param:run


    ___x_cmd_feishu_msg___send_json <<A
{
    "content": "{\"text\":\"text content\"}",
    "msg_type": "$msg_type",
    "message_id": "$message_id"
} 
A
}
