set -e

REGISTRY="registry.keyboardvagabond.com"
VERSION="latest"
PLATFORM="linux/arm64"
IMAGE_NAME="keyboard-vagabond-web"

echo "Building Keyboard Vagabond Landing Page..."

# Ensure dist/ folder exists with optimized files
if [ ! -d "dist" ]; then
    echo "⚠️  No dist/ folder found. Creating optimized build first..."
    if [ -f "package.json" ]; then
        npm run dist-minified
    else
        echo "❌ Error: No package.json found. Run 'npm install' first."
        exit 1
    fi
    echo "✅ Optimized build created in dist/"
fi

docker build \
    --platform $PLATFORM \
    --tag $REGISTRY/library/$IMAGE_NAME:$VERSION \
    --tag $REGISTRY/library/$IMAGE_NAME:latest \
    .

echo "✓ Container built successfully!"

read -p "Push to Harbor registry? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo "Pushing to registry..."
    docker login $REGISTRY
    docker push $REGISTRY/library/$IMAGE_NAME:$VERSION
    docker push $REGISTRY/library/$IMAGE_NAME:latest
    echo "✓ Container pushed successfully!"
fi

echo "🚀 Ready for Kubernetes deployment!"