# shellcheck shell=dash

___x_cmd_claw_cron(){
    [ "$#" -gt 0 ] || set -- --help
    while [ "$#" -gt 0 ]; do
        case "$1" in
            --help|-h)  ___x_cmd help -m claw cron; return 0 ;;
            --)         shift; break ;;
            *)          break ;;
        esac
    done

    local subcmd="$1"
    [ -n "$subcmd" ] || N=claw M="Please provide a subcommand (add|rm|ls|match|run|exec|tz|history)" log:ret:64
    shift

    case "$subcmd" in
        add)    ___x_cmd_claw_cron_add "$@" ;;
        rm)     ___x_cmd_claw_cron_rm "$@" ;;
        ls)     ___x_cmd_claw_cron_ls "$@" ;;
        match)  ___x_cmd_claw_cron_match "$@" ;;
        run)    ___x_cmd_claw_cron_run "$@" ;;
        exec)   ___x_cmd_claw_cron_exec "$@" ;;
        tz)     ___x_cmd_claw_cron_tz "$@" ;;
        history)___x_cmd_claw_cron_history "$@" ;;
        *)      N=claw M="Unknown cron subcmd: $subcmd (expected: add|rm|ls|match|run|exec|tz|history)" log:ret:64 ;;
    esac
}

___x_cmd_claw_cron___helpintercept(){
    while [ "$#" -gt 0 ]; do
        case "$1" in
            --help|-h)  return 0 ;;
            --)         shift; break ;;
            -*)         shift ;;
            *)          break ;;
        esac
    done
    return 1
}

___x_cmd_claw_cron_add(){
    if ___x_cmd_claw_cron___helpintercept "$@"; then
        ___x_cmd help -m claw cron add; return 0
    fi

    local name="" desc="" once_flag=""

    while [ "$#" -gt 0 ]; do
        case "$1" in
            --name)         name="$2"; shift 2 ;;
            --desc)         desc="$2"; shift 2 ;;
            --once)         once_flag="--once"; shift ;;
            --)             shift; break ;;
            -*)             N=claw M="Unknown option: $1" log:ret:64 ;;
            *)              break ;;
        esac
    done

    [ -n "$name" ] || N=claw M="Please provide --name (unique task name)" log:ret:64

    local cron_expr="$1"
    [ -n "$cron_expr" ] || N=claw M="Please provide cron expression (e.g. '0 9 * * *')" log:ret:64
    shift

    local cmd="$*"
    [ -n "$cmd" ] || N=claw M="Please provide the command to execute" log:ret:64

    case "$cmd" in
        "x claw agentrequest"*) ;;
        *)
            claw:warn "Command does not start with 'x claw agentrequest'. Ensure it is executable and does not require a tty."
            ;;
    esac

    ___x_cmd cron add --ns "x-claw-default" --name "$name" ${desc:+"--desc"} ${desc:+"$desc"} $once_flag "$cron_expr" "$cmd"
}

___x_cmd_claw_cron_rm(){
    if ___x_cmd_claw_cron___helpintercept "$@"; then
        ___x_cmd help -m claw cron rm; return 0
    fi
    ___x_cmd cron rm --ns "x-claw-default" "$@"
}

___x_cmd_claw_cron_ls(){
    if ___x_cmd_claw_cron___helpintercept "$@"; then
        ___x_cmd help -m claw cron ls; return 0
    fi
    ___x_cmd cron ls --ns "x-claw-default" "$@"
}

___x_cmd_claw_cron_match(){
    if ___x_cmd_claw_cron___helpintercept "$@"; then
        ___x_cmd help -m claw cron match; return 0
    fi
    ___x_cmd cron match --ns "x-claw-default" "$@"
}

___x_cmd_claw_cron_run(){
    if ___x_cmd_claw_cron___helpintercept "$@"; then
        ___x_cmd help -m claw cron run; return 0
    fi
    ___x_cmd cron run --ns "x-claw-default" "$@"
}

___x_cmd_claw_cron_exec(){
    if ___x_cmd_claw_cron___helpintercept "$@"; then
        ___x_cmd help -m claw cron exec; return 0
    fi
    ___x_cmd cron exec --ns "x-claw-default" "$@"
}

___x_cmd_claw_cron_tz(){
    if ___x_cmd_claw_cron___helpintercept "$@"; then
        ___x_cmd help -m claw cron tz; return 0
    fi
    ___x_cmd cron tz --ns "x-claw-default" "$@"
}

___x_cmd_claw_cron_history(){
    if ___x_cmd_claw_cron___helpintercept "$@"; then
        ___x_cmd help -m claw cron history; return 0
    fi
    ___x_cmd cron history --ns "x-claw-default" "$@"
}
