# shellcheck shell=dash disable=2016,2034

___x_cmd_skill_update(){
    ___x_cmd rmrf "$___X_CMD_SKILL_DATA_DIR/full.hash.txt"
    ___x_cmd_skill___data_install
}

___x_cmd_skill___data_check(){
    if [ ! -f "$___X_CMD_SKILL_FULL_INDEX_FILE" ]; then
        ___x_cmd_skill___data_install || return $?
    else
        ___x_cmd ccmd 7d -- ___x_cmd_skill___data_install
    fi
}

___x_cmd_skill___data_install(){
    local tardir="$___X_CMD_SKILL_DATA_DIR/full"
    local tardir_hash="$___X_CMD_SKILL_DATA_DIR/full.hash.txt"
    local baseurl=
    if ___x_cmd websrc is cn; then
        baseurl="https://codeberg.org/x-cmd/skill/releases/download/latest"
    else
        baseurl="https://github.com/x-cmd/skill/releases/latest/download"
    fi

    local tmpdir="$___X_CMD_ROOT_TMP/skill"
    local tmpfile="$tmpdir/skills.tar.gz"
    local tmphashfile="$tmpdir/skills_hash.txt"
    local url="$baseurl/skills_hash.txt"
    ___x_cmd ensurefp "$tmphashfile"
    ___x_cmd curl -L -sS --speed-time 5 --speed-limit 100 "$url" > "$tmphashfile" || {
        skill:error "Download skills_hash.txt failed"
        ___x_cmd rmrf "$tmphashfile"
        return 1
    }

    local hash; read -r hash < "$tmphashfile"
    local tarhash=""

    if [ -f "$tardir_hash" ]; then
        read -r tarhash < "$tardir_hash"
        skill:debug --targel_hash "$hash" --actual_hash "$tarhash"
        [ "$hash" != "$tarhash" ] || return 0
    fi

    url="$baseurl/skills.tar.gz"
    ___x_cmd rmrf "$tmpfile"
    ___x_cmd ensurefp "$tmpfile"
    skill:info --url "$url" --unpack_dir "$tardir" "Download skills.tar.gz"
    ___x_cmd curl -L -sS --speed-time 5 --speed-limit 100 "$url" > "$tmpfile" || {
        skill:error "Download skills.tar.gz failed"
        ___x_cmd rmrf "$tmpfile"
        return 1
    }
    tarhash="$( ___x_cmd sha512 "$tmpfile" )" || return $?
    [ "$hash" = "$tarhash" ] || {
        ___x_cmd rmrf "$tmpfile"
        skill:error --hash "$hash" --actual_hash "$tarhash" "The downloaded data file is corrupted, hash not match"
        return 1
    }

    ___x_cmd rmrf "$tardir"
    ___x_cmd mkdirp "$tardir"
    ___X_CMD_ZUZ_QUIET=1 ___x_cmd uz "$tmpfile" "$tardir" || return $?
    ___x_cmd ensurefp "$tardir_hash"
    ___x_cmd_cmds cp -f "$tmphashfile" "$tardir_hash"


    local indexfile="$tardir/index.tsv"
    [ -f "$indexfile" ] || {
        ___x_cmd rmrf "$tardir"
        skill:error "The downloaded data file is corrupted, index.tsv not found"
        return 1
    }

    skill:info "x-cmd/skill data update completed"
}

___x_cmd_skill___data_install_from_web(){
    local url="$1"
    local skillid="$2"
    local reference="$3"
    local license="$4"
    local author="$5"

    [ -n "$skillid" ] || N=skill M="Please provide the skill id" log:ret:64

    local datadir="$___X_CMD_SKILL_FULL_DATA_DIR"
    local tardir=""
    local tmpdir="$___X_CMD_ROOT_TMP/skill"
    case "$url" in
        gh:*)
            # gh:anthropics/skills:document-skills/docx
            url="${url#gh:}"
            local repo="${url%%:*}"
            tmpdir="$tmpdir/gh/$repo"
            skill:info ${reference:+--reference} ${reference:+"$reference"} \
                "Downloading skill data from GitHub Repo"

            ___x_cmd rmrf "$tmpdir"                     || return $?
            ___x_cmd gh repo download "$repo" "$tmpdir" || return $?

            local indexfile="$___X_CMD_SKILL_FULL_INDEX_FILE"
            [ -f "$indexfile" ]                         || return 1
            local relative_path=""

            local IFS="$___X_CMD_UNSEENCHAR_HT"
            local x_id=""; local x_url=""; local x_reference=""; local x_license=""; local x_name=""; local x_author=""; local x_description=""
            while read -r x_id x_url x_reference x_license x_name x_author x_description; do
                [ "$x_url" != "${x_url#"gh:${repo}:"}" ] || continue
                x_url="${x_url#gh:}"
                relative_path="${x_url#*:}"
                tardir="$datadir/$x_id"

                skill:debug --id "$x_id" \
                    ${license:+--license} ${license:+"$license"} \
                    ${author:+--author} ${author:+"$author"} \
                    "Moving skill data from GitHub Repo"
                ___x_cmd rmrf "$tardir"                 || return $?
                ___x_cmd mv "$tmpdir/$relative_path" "$tardir"
            done < "$indexfile"
            ;;
        https:*)
            local tarfile="${tmpdir}/${url##*/}"

            skill:info --id "$skillid" \
                ${reference:+--reference} ${reference:+"$reference"} \
                ${license:+--license} ${license:+"$license"} \
                ${author:+--author} ${author:+"$author"} \
                "Downloading skill data from web URL"

            ___x_cmd ensurefp "$tarfile"                || return $?
            ___x_cmd curl -L -sS --speed-time 5 --speed-limit 100 "$url" > "$tarfile" || {
                skill:error --url "$url" "Download ${url##*/} failed"
                ___x_cmd rmrf "$tarfile"
                return 1
            }

            tardir="$datadir/$skillid"
            ___x_cmd rmrf "$tardir"                    || return $?
            ___x_cmd mkdirp "$tardir"                  || return $?
            ___X_CMD_ZUZ_QUIET=1 ___x_cmd uz "$tarfile" "$tardir"
            ;;
        *)  skill:error --id "$skillid" --url "$url" "Unknown download method"
            return 1
            ;;
    esac
}

___x_cmd_skill___data_get_index_record_(){
    local entryid="$1"
    local indexfile="$___X_CMD_SKILL_FULL_INDEX_FILE"
    [ -f "$indexfile" ] || return 1
    local IFS="$___X_CMD_UNSEENCHAR_HT"
    while read -r x_id x_url x_reference x_license x_name x_author x_description; do
        [ "$x_id" != "$entryid" ] || return 0
    done < "$indexfile"
    return 1
}
