#!/bin/bash
# Complete publish script for webpage_quality_analyzer v1.0.2
# Publishes to: crates.io, npm, and builds binaries for GitHub releases

set -e  # Exit on error

VERSION="1.0.2"
REPO_ROOT="/home/gyashu/projects/rust/webpage-quality-analyser"

echo "🚀 Publishing webpage_quality_analyzer v${VERSION} to all platforms..."
echo "=================================================="
echo ""

# Change to repository root
cd "$REPO_ROOT"

# Step 1: Verify we're on the right version
echo "📋 Step 1: Verifying version numbers..."
CARGO_VERSION=$(grep '^version = ' Cargo.toml | head -1 | sed 's/version = "\(.*\)"/\1/')
if [ "$CARGO_VERSION" != "$VERSION" ]; then
    echo "❌ Error: Cargo.toml version ($CARGO_VERSION) doesn't match target version ($VERSION)"
    exit 1
fi
echo "✅ Version verified: $VERSION"
echo ""

# Step 2: Run tests
echo "🧪 Step 2: Running tests..."
cargo test --all-features --lib -- --test-threads=1
if [ $? -ne 0 ]; then
    echo "❌ Tests failed! Fix errors before publishing."
    exit 1
fi
echo "✅ All tests passed"
echo ""

# Step 3: Clean build
echo "🧹 Step 3: Cleaning previous builds..."
cargo clean
rm -rf pkg/
echo "✅ Clean complete"
echo ""

# Step 4: Build Rust library
echo "🦀 Step 4: Building Rust library (release mode)..."
cargo build --release --lib
if [ $? -ne 0 ]; then
    echo "❌ Rust build failed!"
    exit 1
fi
echo "✅ Rust library built"
echo ""

# Step 5: Publish to crates.io
echo "📦 Step 5: Publishing to crates.io..."
echo "⚠️  This will publish webpage_quality_analyzer v${VERSION} to crates.io"
read -p "Continue? (y/n) " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
    cargo publish
    if [ $? -ne 0 ]; then
        echo "❌ crates.io publish failed!"
        exit 1
    fi
    echo "✅ Published to crates.io"
    echo "   Users can install with: cargo add webpage_quality_analyzer"
    sleep 5  # Give crates.io time to process
else
    echo "⏭️  Skipping crates.io publish"
fi
echo ""

# Step 6: Build WASM
echo "🌐 Step 6: Building WebAssembly..."
wasm-pack build --target bundler --no-default-features --features wasm --release
if [ $? -ne 0 ]; then
    echo "❌ WASM build failed!"
    exit 1
fi
echo "✅ WASM built successfully"
echo ""

# Step 7: Test WASM
echo "🧪 Step 7: Testing WASM exports..."
node test_version.mjs
if [ $? -ne 0 ]; then
    echo "❌ WASM version test failed!"
    exit 1
fi
echo "✅ WASM tests passed"
echo ""

# Step 8: Publish to npm
echo "📦 Step 8: Publishing to npm..."
echo "⚠️  This will publish webpage_quality_analyzer v${VERSION} to npm"
read -p "Continue? (y/n) " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
    cd pkg
    npm publish
    if [ $? -ne 0 ]; then
        echo "❌ npm publish failed!"
        exit 1
    fi
    cd ..
    echo "✅ Published to npm"
    echo "   Users can install with: npm install webpage_quality_analyzer"
else
    echo "⏭️  Skipping npm publish"
fi
echo ""

# Step 9: Build CLI binary
echo "🔧 Step 9: Building CLI binary..."
cargo build --release --bin wqa --features cli
if [ $? -ne 0 ]; then
    echo "❌ CLI build failed!"
    exit 1
fi
strip target/release/wqa 2>/dev/null || true
echo "✅ CLI binary built"
./target/release/wqa --version
echo ""

# Step 10: Prepare CLI release
echo "📦 Step 10: Preparing CLI release package..."
mkdir -p releases/v${VERSION}/linux-x64
cp target/release/wqa releases/v${VERSION}/linux-x64/
chmod +x releases/v${VERSION}/linux-x64/wqa
echo "✅ CLI binary copied to releases/v${VERSION}/linux-x64/wqa"
echo ""

# Step 11: Build C++ FFI package
echo "🔨 Step 11: Building C++ FFI package..."
cargo build --release --features ffi
if [ $? -ne 0 ]; then
    echo "❌ C++ FFI build failed!"
    exit 1
fi

# Create C++ distribution package
mkdir -p cpp-package/{lib,include,examples}
cp target/release/libwebpage_quality_analyzer.so cpp-package/lib/ 2>/dev/null || true
cp target/release/libwebpage_quality_analyzer.a cpp-package/lib/ 2>/dev/null || true
cp bindings/cpp/webpage_quality_analyzer.hpp cpp-package/include/
cp -r bindings/cpp/examples cpp-package/
cp bindings/cpp/README.md cpp-package/

# Create tarball
cd cpp-package
tar -czf ../releases/v${VERSION}/webpage-quality-analyzer-cpp-v${VERSION}-linux-x64.tar.gz .
cd ..
echo "✅ C++ package created: releases/v${VERSION}/webpage-quality-analyzer-cpp-v${VERSION}-linux-x64.tar.gz"
echo ""

# Step 12: Create git tag
echo "🏷️  Step 12: Creating git tag..."
if git rev-parse "v${VERSION}" >/dev/null 2>&1; then
    echo "⚠️  Tag v${VERSION} already exists"
else
    read -p "Create git tag v${VERSION}? (y/n) " -n 1 -r
    echo ""
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        git tag -a "v${VERSION}" -m "Release v${VERSION} - Profile scoring improvements"
        echo "✅ Git tag created: v${VERSION}"
        echo ""
        read -p "Push tag to origin? (y/n) " -n 1 -r
        echo ""
        if [[ $REPLY =~ ^[Yy]$ ]]; then
            git push origin "v${VERSION}"
            echo "✅ Tag pushed to origin"
        fi
    fi
fi
echo ""

# Summary
echo "=================================================="
echo "✨ Build Summary for v${VERSION}"
echo "=================================================="
echo ""
echo "📦 Packages:"
echo "   ✅ Rust crate: published to crates.io"
echo "   ✅ npm package: published to npm"
echo "   ✅ CLI binary: releases/v${VERSION}/linux-x64/wqa"
echo "   ✅ C++ package: releases/v${VERSION}/webpage-quality-analyzer-cpp-v${VERSION}-linux-x64.tar.gz"
echo ""
echo "🔧 Files ready for GitHub release:"
echo "   - releases/v${VERSION}/linux-x64/wqa"
echo "   - releases/v${VERSION}/webpage-quality-analyzer-cpp-v${VERSION}-linux-x64.tar.gz"
echo ""
echo "📝 Next steps:"
echo "   1. Go to: https://github.com/NotGyashu/webpage-quality-analyser/releases/new"
echo "   2. Choose tag: v${VERSION}"
echo "   3. Upload binaries from releases/v${VERSION}/"
echo "   4. Copy CHANGELOG.md content to release notes"
echo "   5. Publish the release"
echo ""
echo "🎉 Done! All platforms published successfully!"
