# shellcheck shell=dash

___x_cmd_feishu_abot_service(){
    param:void

    [ $# -gt 0 ]    ||      set -- status

    local op="$1";          shift
    case "$op" in
        -h|--help)          ___x_cmd help -m feishu abot service "$@" ; return 0 ;;
        start|stop|restart|status|ensure|log|run)
                            ___x_cmd_feishu_abot_service_"$op" "$@"      ;;
        *)                  N=feishu M="Unknown op -> $op" log:ret:64   ;;
    esac
}

# x feishu abot service start --admin
# x feishu abot service start --group
# x feishu abot service start --group <chat_id>

___x_cmd_feishu_abot_service_start(){
    local chatid=;
    local chat_type="admin"
    local sname="x-feishu-admin-listener"
    while [ $# -gt 0 ]; do
        case "$1" in
            -h|--help)  ___x_cmd help -m feishu abot service start "$@" ; return 0 ;;
            --admin)    shift ;;
            --group)
                        chat_type="group"
                        if [ -n "$2" ]; then
                            chatid="$2"
                            shift 2
                            sname="x-feishu-group-${chatid}-listener"
                            ! ___x_cmd service isalive "x-feishu-group-listener" || {
                                feishu:info "Service x-feishu-group-listener is already running and listens to all groups, no need to start a separate service for [chat_id=$chatid]"
                                return 0
                            }
                        else
                            shift
                            sname="x-feishu-group-listener"
                        fi
                        ;;
            *)          N=feishu M="Unknown op -> $1" log:ret:64   ;;
        esac
    done

    ___x_cmd_feishu_ensureinit || return "$?"

    [ "$chat_type" != "admin" ] || {
        chatid=""
        ___x_cmd_feishu_cur chatid:= 2>/dev/null
        [ -n "$chatid" ] || {
            feishu:info "Please set your default chat id first"
            ___x_cmd_feishu_init_chatid || return $?
            ___x_cmd_feishu_cur chatid:= 2>/dev/null
            [ -n "$chatid" ] || N=feishu M="Not found default chat id" log:ret:1
        }
    }

    ___x_cmd service start      "$sname"   --   x-cmd feishu abot service run "--${chat_type}" "${chatid:+"$chatid"}" "$@"

    ___x_cmd_cmds sleep 2
    ___x_cmd service isalive "$sname" || {
        feishu:error "Failed to start the feishu bot service ==> $sname"
        ___x_cmd service log tail "$sname"
        return 1
    }

    feishu:info "Feishu bot service started successfully ==> $sname"
}

___x_cmd_feishu_abot_service_ensure(){
    local chat_type="admin"
    local sname="x-feishu-admin-listener"
    local chatid=""
    while [ $# -gt 0 ]; do
        case "$1" in
            -h|--help)  ___x_cmd help -m feishu abot service ensure "$@" ; return 0 ;;
            --admin)    shift ;;
            --group)
                        chat_type="group"
                        if [ -n "$2" ]; then
                            chatid="$2"
                            shift 2
                            sname="x-feishu-group-${chatid}-listener"
                        else
                            shift
                            sname="x-feishu-group-listener"
                        fi
                        ;;
            *)          N=feishu M="Unknown op -> $1" log:ret:64   ;;
        esac
    done

    ___x_cmd_feishu_ensureinit || return "$?"

    [ "$chat_type" != "admin" ] || {
        chatid=""
        ___x_cmd_feishu_cur chatid:= 2>/dev/null
        [ -n "$chatid" ] || {
            feishu:info "Please set your default chat id first"
            ___x_cmd_feishu_init_chatid || return $?
            ___x_cmd_feishu_cur chatid:= 2>/dev/null
            [ -n "$chatid" ] || N=feishu M="Not found default chat id" log:ret:1
        }
    }

    ! ___x_cmd service isalive "$sname" || return 0

    local i=0
    while [ $((i=i+1)) -le 3 ]; do
        if [ "$i" -gt 1 ]; then
            feishu:warn "Failed to start service, retrying"
        fi
        if [ "$chat_type" = "group" ] && [ -n "$chatid" ]; then
            ___x_cmd_feishu_abot_service_start "--group" "$chatid"
        else
            ___x_cmd_feishu_abot_service_start "--${chat_type}"
        fi
        ___x_cmd sleep 2
        ! ___x_cmd service isalive "$sname"   || return 0
    done

    feishu:error "Service cannot started -> $sname"
    ___x_cmd service log tail "$sname"
    return 1
}

___x_cmd_feishu_abot_service_restart(){
    local chat_type="admin"
    local chatid=""
    while [ $# -gt 0 ]; do
        case "$1" in
            -h|--help)  ___x_cmd help -m feishu abot service restart "$@" ; return 0 ;;
            --admin)    shift ;;
            --group)
                        chat_type="group"
                        if [ -n "$2" ]; then
                            chatid="$2"
                            shift 2
                            sname="x-feishu-group-${chatid}-listener"
                        else
                            shift
                            sname="x-feishu-group-listener"
                        fi
                        ;;
            *)          N=feishu M="Unknown op -> $1" log:ret:64   ;;
        esac
    done

    ___x_cmd_feishu_ensureinit || return "$?"

    ___x_cmd_feishu_abot_service_stop  --"${chat_type}" "${chatid:+"$chatid"}" || return $?
    ___x_cmd_feishu_abot_service_start --"${chat_type}" "${chatid:+"$chatid"}"
}

___x_cmd_feishu_abot_service_stop(){
    local chat_type="admin"
    local sname="x-feishu-admin-listener"
    local chatid=""
    while [ $# -gt 0 ]; do
        case "$1" in
            -h|--help)  ___x_cmd help -m feishu abot service stop "$@" ; return 0 ;;
            --admin)    shift ;;
            --group)
                        chat_type="group"
                        if [ -n "$2" ]; then
                            chatid="$2"
                            shift 2
                            sname="x-feishu-group-${chatid}-listener"
                        else
                            shift
                            sname="x-feishu-group-listener"
                        fi
                        ;;
            *)          N=feishu M="Unknown op -> $1" log:ret:64   ;;
        esac
    done

    ___x_cmd service stop   "$sname"
}

___x_cmd_feishu_abot_service_status(){
    local chat_type="admin"
    local sname="x-feishu-admin-listener"
    local chatid=""
    while [ $# -gt 0 ]; do
        case "$1" in
            -h|--help)      ___x_cmd help -m feishu abot service status "$@" ; return 0 ;;
            --admin)        shift ;;
            --group)
                            chat_type="group"
                            if [ -n "$2" ]; then
                                chatid="$2"
                                shift 2
                                sname="x-feishu-group-${chatid}-listener"
                            else
                                shift
                                sname="x-feishu-group-listener"
                            fi
                            ;;
            x-feishu-*)
                            sname="$1"
                            shift
                            ;;
            *)          N=feishu M="Unknown op -> $1" log:ret:64   ;;
        esac
    done
    ___x_cmd service status   "$sname"
}

___x_cmd_feishu_abot_service_log(){
    local chatid="$1"
    [ -n "$chatid" ] || {
        ___x_cmd_feishu_cur chatid:= 2>/dev/null
        [ -n "$chatid" ] || {
            feishu:error "Please set your default chat id first ==> 'x feishu init'"
            return 1
        }
    }

    local fp
    local x_=; ___x_cmd_feishu_abot_data_which_ "$chatid"
    fp="$x_"
    [ -n "$fp" ] || N=feishu M="Message data file not found for [chatid=$chatid]" log:ret:1

    local ___X_CMD_TAIL_MAIN_OPTS=

    while [ $# -gt 0 ]; do
        case "$1" in
            -n)     ___x_cmd cmdstr ___X_CMD_TAIL_MAIN_OPTS -n "$1";  arg:2:shift   ;;
            -f)     ___x_cmd cmdstr ___X_CMD_TAIL_MAIN_OPTS "$1";     shift         ;;
            *)      break       ;;
        esac
    done

    eval set -- "$___X_CMD_TAIL_MAIN_OPTS" "\"\$@\""

    ___x_cmd_cmds tail "$@" "$fp"
}

___x_cmd_feishu_abot_service_run(){
    ___x_cmd feishu listen --fp "$@" || return $?
}
