#!/bin/bash

# Download dshash utility
TEMP_DIR=$(mktemp -d)
trap "rm -rf $TEMP_DIR" EXIT
DSHASH="$TEMP_DIR/dshash"

echo "Downloading dshash utility..."
ARCH=$(uname -m)
if [ "$ARCH" = "x86_64" ]; then
    ARCH="x86_64"
elif [ "$ARCH" = "aarch64" ] || [ "$ARCH" = "arm64" ]; then
    ARCH="aarch64"
else
    echo "Unsupported architecture: $ARCH"
    exit 1
fi

curl -L -o "$DSHASH" "https://getbin.xyz/dshash:latest-$ARCH" 2>/dev/null || { echo "Failed to download dshash"; exit 1; }
chmod +x "$DSHASH"
echo "dshash downloaded successfully"

# read ~/.config/simple_object_storage/sos_config.json
CONFIG_PATH="./sos_config.json"
if [ ! -f "${CONFIG_PATH}" ]; then
    echo "config file not found at ${CONFIG_PATH}"
    exit 1
fi
CONFIG=$(cat "${CONFIG_PATH}")


# Create a temporary test file (1024MB!)
echo "Creating test file..."
dd if=/dev/urandom of=test_file.bin bs=1M count=1024

# Calculate original file hash
echo "Calculating original file hash..."
ORIGINAL_HASH=$("$DSHASH" test_file.bin)
echo "Original hash: $ORIGINAL_HASH"

# get the host and port from the config
HOST=$(echo "$CONFIG" | jq -r '.host')
PORT=$(echo "$CONFIG" | jq -r '.port')

# Use plaintext tokens from environment (set by generate_test_config.sh or manually)
# The config file should only contain hashed tokens
if [ -n "${TEST_TOKEN1:-}" ] && [ -n "${TEST_TOKEN2:-}" ] && [ -n "${TEST_TOKEN3:-}" ]; then
    # Use environment tokens (plaintext)
    TOKENS=("$TEST_TOKEN1" "$TEST_TOKEN2" "$TEST_TOKEN3")
    TOKEN_COUNT=${#TOKENS[@]}
    RANDOM_INDEX=$((RANDOM % TOKEN_COUNT))
    WRITE_TOKEN="${TOKENS[$RANDOM_INDEX]}"
    echo "Using plaintext token index $RANDOM_INDEX from environment"
else
    # For static test configs, use hardcoded plaintext tokens
    # These correspond to the hashes in the static sos_config.json
    TOKENS=("t570H7DmK2VBfCwUmtFaUXyzVklL90E1" "U3x9V39Y7rjXdRK0oxZsCz5lD6jFFDtm" "UhtchhGDEGXlJ37GumimFtPe0imjAvak")
    TOKEN_COUNT=${#TOKENS[@]}
    RANDOM_INDEX=$((RANDOM % TOKEN_COUNT))
    WRITE_TOKEN="${TOKENS[$RANDOM_INDEX]}"
    echo "Using hardcoded plaintext token index $RANDOM_INDEX (for static config)"
fi

# Upload the file
echo "Uploading file..."
RESPONSE=$(curl -X PUT \
    -H "Authorization: Bearer ${WRITE_TOKEN}" \
    -F "file=@test_file.bin" \
    -F 'metadata={"labeltags":["test:latest","test:large"],"description":"Test file"}' \
    "http://${HOST}:${PORT}/upload")
echo "Upload response: $RESPONSE"

# Extract the hash from the response
HASH=$(echo "$RESPONSE" | jq -r '.hash')
echo "Hash: $HASH"

# Check if the file exists by hash
echo "Checking if file exists by hash..."
EXISTS_HASH=$(curl "http://${HOST}:${PORT}/exists/$HASH")
echo "Exists by hash response: $EXISTS_HASH"

# Check if the file exists by label:tag
echo "Checking if file exists by label:tag..."
EXISTS_TAG=$(curl "http://${HOST}:${PORT}/exists/test:latest")
echo "Exists by label:tag response: $EXISTS_TAG"

# Download the file by hash
echo "Downloading file by hash..."
curl -o downloaded_by_hash.bin "http://${HOST}:${PORT}/object/$HASH"

# Download the file by label:tag
echo "Downloading file by label:tag..."
curl -o downloaded_by_tag.bin "http://${HOST}:${PORT}/object/test:latest"

# Verify downloaded files
echo "Verifying downloaded files..."
HASH_BY_HASH=$("$DSHASH" downloaded_by_hash.bin)
HASH_BY_TAG=$("$DSHASH" downloaded_by_tag.bin)

echo "Original hash: $ORIGINAL_HASH"
echo "Hash of file downloaded by hash: $HASH_BY_HASH"
echo "Hash of file downloaded by tag: $HASH_BY_TAG"

if [ "$ORIGINAL_HASH" = "$HASH_BY_HASH" ] && [ "$ORIGINAL_HASH" = "$HASH_BY_TAG" ]; then
    echo "Hash verification successful!"
else
    echo "Hash verification failed!"
    exit 1
fi

# delete the file!
echo "Deleting file..."
DELETE_RESPONSE=$(curl -H "Authorization: Bearer ${WRITE_TOKEN}" \
    "http://${HOST}:${PORT}/deleteobject?hash=${HASH}")
echo "Delete response: $DELETE_RESPONSE"

# Clean up
echo "Cleaning up..."
rm test_file.bin downloaded_by_hash.bin downloaded_by_tag.bin

echo "Test completed." 