# shellcheck shell=dash

___x_cmd_hub_file_share(){
    local X_help_cmd='___x_cmd_hub___help file share'
    help:arg-null:parse

    local op="$1"
    case "$op" in
        ls|list)      shift; ___x_cmd_hub_file_share_ls "$@"      ; return ;;
        rm|delete)    shift; ___x_cmd_hub_file_share_rm "$@"      ; return ;;
        update)       shift; ___x_cmd_hub_file_share_update "$@"  ; return ;;
        zip)          shift; ___x_cmd_hub_file_share_zip "$@"     ; return ;;
    esac

    # Default: create share
    ___x_cmd_hub_file_share_create "$@"
}

___x_cmd_hub_file_share_create(){
    local expiresIn=
    local requireLogin=
    local file_path=
    local file_space=
    local password=
    while [ $# -gt 0 ]; do
        case "$1" in
            --expires|-e)       expiresIn="$2";      shift 2 ;;
            --require-login)    requireLogin=true;    shift ;;
            --path)             file_path="$2";       shift 2 ;;
            --space)            file_space="$2";      shift 2 ;;
            --password)         password="$2";        shift 2 ;;
            *)                  break ;;
        esac
    done

    local remotefp="$1"
    [ -n "$remotefp" ] || { ___x_cmd_ui_tf false "Remote file path is required"; return 1; }
    remotefp="$(___x_cmd_hub_file_normalize_remotefp "$remotefp")"

    local data
    data="$(___x_cmd jo dict ${file_path:+"path=$file_path"} ${file_space:+"space=$file_space"} ${expiresIn:+"expiresIn=$expiresIn"} ${requireLogin:+"requireLogin=$requireLogin"} ${password:+"password=$password"})"

    local resp=
    resp="$(___x_cmd_hub_u_curl post "/api/v0/file/share?res=$(___x_cmd url encode "$remotefp")" data)" || {
        ___x_cmd_hub_u_handle_resp false "Failed to create share for $remotefp"
        return 1
    }

    ___x_cmd_hub_u_handle_resp true "Share created successfully:"
    printf "%s" "$resp" | {
        local id=; local spaceId=; local spaceName=; local filePath=; local url=;
        ___x_cmd jo env . .id .spaceId .spaceName .filePath .url
        printf "  - ID:         %s\n" "$id"
        printf "  - Space ID:   %s\n" "$spaceId"
        printf "  - Space Name: %s\n" "$spaceName"
        printf "  - File Path:  %s\n" "$filePath"
        printf "  - Share URL:  %s\n" "$url"
    }
}

___x_cmd_hub_file_share_ls(){
    local X_help_cmd='___x_cmd_hub___help file share ls'
    help:arg:parse

    local json=
    local page=1
    local limit=20
    while [ $# -gt 0 ]; do
        case "$1" in
            --json|-j)      json=true;      shift ;;
            --page|-p)      page="$2";       shift 2 ;;
            --limit|-l)     limit="$2";      shift 2 ;;
            *)              break ;;
        esac
    done

    local resp=
    resp="$(___x_cmd_hub_u_curl get "/api/v0/file/share/list?page=${page}&limit=${limit}")" || {
        ___x_cmd_hub_u_handle_resp false "Failed to list shares:"
        return 1
    }

    [ -z "$json" ] || { printf "%s\n" "$resp"; return 0; }

    printf "%s" "$resp" | {
        local total=;
        local list=;
        ___x_cmd jo env . .total .list
        [ "$total" = "0" ] && { printf "No shares found.\n"; return 0; }
        printf "Total: %s\n\n" "$total"
        printf "%s" "$list" | \
            ___x_cmd jo 2c .id .spaceId .spaceName .filePath .size .viewCount .expiresAt .createTime | \
            ___x_cmd csv header --add id spaceId spaceName filePath size viewCount expiresAt createTime | \
            ___x_cmd_hub_u_st_tab 10,15,15,25,10,10,20,20
    }
}

___x_cmd_hub_file_share_rm(){
    local X_help_cmd='___x_cmd_hub___help file share rm'
    help:arg:parse

    local id="$1"
    [ -n "$id" ] || { ___x_cmd_ui_tf false "File ID is required"; return 1; }

    local resp=
    resp="$(___x_cmd_hub_u_curl delete "/api/v0/file/share/$(___x_cmd url encode "$id")")" || {
        ___x_cmd_hub_u_handle_resp false "Failed to delete share $id"
        return 1
    }

    ___x_cmd_hub_u_handle_resp true "Share deleted successfully"
}

___x_cmd_hub_file_share_update(){
    local X_help_cmd='___x_cmd_hub___help file share update'
    help:arg:parse
    local requireLogin=
    local expiresIn=
    local password=
    while [ $# -gt 0 ]; do
        case "$1" in
            --require-login)    requireLogin=true;       shift ;;
            --expires|-e)       expiresIn="$2";          shift 2 ;;
            --password)         password="$2";            shift 2 ;;
            --remove-password)  password=null;     shift ;;
            *)                  break ;;
        esac
    done

    local id="$1"
    [ -n "$id" ] || { ___x_cmd_ui_tf false "File ID is required"; return 1; }

    # Build JSON body
    [ -n "$requireLogin" ] || [ -n "$expiresIn" ] || [ -n "$password" ] || {
        ___x_cmd_ui_tf false "At least one option is required (--require-login, --expires, --password, --remove-password)"
        return 1
    }
    local data
    data="$(___x_cmd jo dict ${requireLogin:+"requireLogin=$requireLogin"} ${expiresIn:+"expiresIn=$expiresIn"} ${password:+"password=$password"})"

    local resp=
    resp="$(___x_cmd_hub_u_curl patch "/api/v0/file/share/$(___x_cmd url encode "$id")" data)" || {
        ___x_cmd_hub_u_handle_resp false "Failed to update share $id"
        return 1
    }

    ___x_cmd_hub_u_handle_resp true "Share updated successfully"
}

___x_cmd_hub_file_share_zip(){
    local X_help_cmd='___x_cmd_hub___help file share zip'
    help:arg:parse

    local expiresIn=
    local requireLogin=
    local entryPath=
    local space=
    local password=
    while [ $# -gt 0 ]; do
        case "$1" in
            --expires|-e)       expiresIn="$2";      shift 2 ;;
            --require-login)    requireLogin=true;   shift   ;;
            --entry)            entryPath="$2";      shift 2 ;;
            --space)            space="$2";           shift 2 ;;
            --password)         password="$2";        shift 2 ;;
            *)                  break ;;
        esac
    done

    local remotefp="$1"
    [ -n "$remotefp" ] || { ___x_cmd_ui_tf false "Remote file path is required"; return 1; }
    remotefp="$(___x_cmd_hub_file_normalize_remotefp "$remotefp")"

    local data
    data="$(___x_cmd jo dict ${space:+"space=$space"} ${entryPath:+"entryPath=$entryPath"} ${expiresIn:+"expiresIn=$expiresIn"} ${requireLogin:+"requireLogin=$requireLogin"} ${password:+"password=$password"})"

    local resp=
    resp="$(___x_cmd_hub_u_curl post "/api/v0/file/share/zip?res=$(___x_cmd url encode "$remotefp")" data)" || {
        ___x_cmd_hub_u_handle_resp false "Failed to create zip share for $remotefp"
        return 1
    }

    ___x_cmd_hub_u_handle_resp true "Zip share created successfully:"
    printf "%s" "$resp" | {
        local spaceId=; local spaceName=; local url=; local entryFile=; local totalFiles=; local files=
        ___x_cmd jo env . .spaceId .spaceName .url .entryFile .totalFiles .files
        printf "  - Space ID:    %s\n" "$spaceId"
        printf "  - Space Name:  %s\n" "$spaceName"
        printf "  - Share URL:   %s\n" "$url"
        printf "  - Entry file:  %s\n" "$entryFile"
        printf "  - Total files: %s\n" "$totalFiles"
        [ -z "$files" ] || printf "  - Files:       %s\n" "$files"
    }
}
