#!/bin/bash

# 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=$(sha256sum test_file.bin | cut -d' ' -f1)
echo "Original hash: $ORIGINAL_HASH"

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

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

# extract the first write token from the config
WRITE_TOKEN=$(echo $CONFIG | jq -r '.write_tokens[0]')

# 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=$(sha256sum downloaded_by_hash.bin | cut -d' ' -f1)
HASH_BY_TAG=$(sha256sum downloaded_by_tag.bin | cut -d' ' -f1)

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." 