#!/bin/bash

# Pre-release checklist script
set -e

echo "🔍 Claude-Utils Pre-Release Check"
echo "================================="
echo ""

# Check if Rust/Cargo available
if ! command -v cargo &> /dev/null; then
    echo "⚠️  Cargo not found. Rust needs to be installed."
    echo ""
    echo "To install Rust:"
    echo "1. Run: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh"
    echo "2. Follow the installation prompts"
    echo "3. Run: source $HOME/.cargo/env"
    echo "4. Then run this script again"
    echo ""
    echo "Continuing with other checks..."
    echo ""
    CARGO_AVAILABLE=false
else
    CARGO_AVAILABLE=true
fi

if [ "$CARGO_AVAILABLE" = true ]; then
    # Check compilation
    echo "1️⃣ Checking if code compiles..."
    cargo check --all-targets
    echo "✅ Compilation successful"

    # Run tests
    echo ""
    echo "2️⃣ Running tests..."
    cargo test --quiet
    echo "✅ All tests passed"

    # Check formatting
    echo ""
    echo "3️⃣ Checking code formatting..."
    if ! cargo fmt -- --check; then
        echo "❌ Code needs formatting. Run: cargo fmt"
        exit 1
    fi
    echo "✅ Code is properly formatted"

    # Run clippy
    echo ""
    echo "4️⃣ Running clippy..."
    cargo clippy -- -D warnings
    echo "✅ No clippy warnings"

    # Build release
    echo ""
    echo "5️⃣ Building release binary..."
    cargo build --release
    echo "✅ Release build successful"

    # Check binary
    echo ""
    echo "6️⃣ Testing binary..."
    ./target/release/claude-utils --version
    ./target/release/claude-utils --help > /dev/null
    echo "✅ Binary works"
else
    echo "⏭️  Skipping Rust-specific checks"
fi

# Check for sensitive data
echo ""
echo "7️⃣ Checking for sensitive data..."
if grep -r "SECRET\|PASSWORD\|TOKEN\|KEY" --exclude-dir=target --exclude-dir=.git --exclude="*.md" --exclude="pre-release-check.sh" .; then
    echo "⚠️  Warning: Found potential sensitive data in code"
else
    echo "✅ No sensitive data found"
fi

# Check required files
echo ""
echo "8️⃣ Checking required files..."
required_files=("README.md" "LICENSE" "CHANGELOG.md" "Cargo.toml" ".gitignore")
for file in "${required_files[@]}"; do
    if [ ! -f "$file" ]; then
        echo "❌ Missing required file: $file"
        exit 1
    fi
done
echo "✅ All required files present"

# Summary
echo ""
echo "✅ Pre-release check complete!"
echo ""
echo "Next steps:"
echo "1. Create GitHub repository"
echo "2. Run: ./setup-git.sh"
echo "3. Push to GitHub"
echo "4. Create release tag"