# shellcheck shell=dash

# GET /api/v1/skills/{slug}/file?path=&version=&tag=
___x_cmd_clawhub_skill_file(){
    case "$1" in -h|--help) shift; ___x_cmd help -m clawhub skill file "$@" ; return 0 ;; esac

    local slug="$1"; [ -n "$slug" ] || M='Provide skill slug' N=clawhub log:ret:64
    shift

    local filepath=; local version=; local tag=
    while [ $# -gt 0 ]; do
        case "$1" in
            --filepath) filepath="$2";  shift 2 ;;
            --version)  version="$2";   shift 2 ;;
            --tag)      tag="$2";       shift 2 ;;
            *) break ;;
        esac
    done

    [ -n "$filepath" ] || M='Provide file path (--filepath)' N=clawhub log:ret:64

    local qs="path=$(___x_cmd url encode "$filepath")"
    [ -z "$version" ] || qs="$qs&version=$version"
    [ -z "$tag" ]     || qs="$qs&tag=$tag"

    local resp=
    resp="$(___x_cmd_clawhub_u_curl get "/api/v1/skills/$(___x_cmd url encode "$slug")/file?$qs" --no-auth)" || {
        ___x_cmd_clawhub_u_handle_resp false "Failed to get file '$filepath' from '$slug':"
        return 1
    }
    printf "%s\n" "$resp"
}
