#!/bin/bash
set -e

echo "Building wheels for all platforms using Python 3.12..."
echo ""

# Check Docker is running
if ! docker info > /dev/null 2>&1; then
    echo "ERROR: Docker is not running. Please start Docker Desktop."
    exit 1
fi

# 1. macOS ARM64 (via Docker with Python 3.12)
echo "==> Building macOS ARM64 wheels (Docker + Python 3.12)..."
docker run --rm \
  --platform linux/arm64 \
  -v $(pwd):/work \
  -w /work \
  python:3.12-slim \
  bash -c "
    apt-get update -qq && apt-get install -y -qq gcc g++ make libffi-dev > /dev/null
    pip install --upgrade pip > /dev/null
    mkdir -p vendor/macos-arm64-docker
    pip download --dest vendor/macos-arm64-docker -r requirements.txt
    pip wheel --wheel-dir vendor/macos-arm64-docker --no-deps vendor/macos-arm64-docker/*.tar.gz 2>/dev/null || true
    rm -f vendor/macos-arm64-docker/*.tar.gz
  "
echo "✓ macOS ARM64: $(ls vendor/macos-arm64-docker/*.whl 2>/dev/null | wc -l | xargs) wheels"

# Actually, let's use native Mac Python 3.12 if available, otherwise use what we have
echo ""
echo "==> Building macOS ARM64 wheels (native - using your .venv312)..."
mkdir -p vendor/macos-arm64

# Use the Python from your working venv
if [ -f ".venv312/bin/pip" ]; then
    echo "Using Python from .venv312..."
    .venv312/bin/pip download --dest vendor/macos-arm64 -r requirements.txt
    echo "✓ macOS ARM64: $(ls vendor/macos-arm64/*.whl 2>/dev/null | wc -l | xargs) wheels"
else
    echo "ERROR: .venv312 not found!"
    echo "Please activate your working venv first:"
    echo "  source .venv312/bin/activate"
    echo "  pip download --dest vendor/macos-arm64 -r requirements.txt"
    exit 1
fi

# 2. Linux x86_64 (via Docker)
echo ""
echo "==> Building Linux x86_64 wheels (Docker + Python 3.12)..."
docker run --rm \
  -v $(pwd):/work \
  -w /work \
  python:3.12-slim \
  bash -c "
    apt-get update -qq && apt-get install -y -qq gcc g++ make libffi-dev > /dev/null
    pip install --upgrade pip > /dev/null
    mkdir -p vendor/linux-x86_64
    pip download --dest vendor/linux-x86_64 -r requirements.txt
    pip wheel --wheel-dir vendor/linux-x86_64 --no-deps vendor/linux-x86_64/*.tar.gz 2>/dev/null || true
    rm -f vendor/linux-x86_64/*.tar.gz
  "
echo "✓ Linux x86_64: $(ls vendor/linux-x86_64/*.whl 2>/dev/null | wc -l | xargs) wheels"

# 3. Generate SHA256 checksums
echo ""
echo "==> Generating checksums..."
for platform in macos-arm64 linux-x86_64; do
  if [ -d "vendor/$platform" ] && [ "$(ls -A vendor/$platform/*.whl 2>/dev/null)" ]; then
    (cd vendor/$platform && shasum -a 256 *.whl > SHA256SUMS)
    echo "✓ Checksums for $platform"
  fi
done

# 4. Create provenance file
echo ""
echo "==> Creating provenance documentation..."
cat > vendor/PROVENANCE.md << EOF
# Dependency Provenance

All wheels generated on: $(date -u +"%Y-%m-%d %H:%M:%S UTC")
Python version: 3.12
Build machine: $(uname -s) $(uname -m)
Docker used: Yes (python:3.12-slim)

## Platforms
- macOS ARM64: Built using .venv312 (Python 3.12)
- Linux x86_64: Built using Docker python:3.12-slim

## Package Versions
$(cat requirements.txt | head -20)

## Integrity Verification
Each platform directory contains SHA256SUMS for verification:
\`\`\`bash
cd vendor/linux-x86_64
shasum -a 256 -c SHA256SUMS
\`\`\`

Last updated: $(date -u +"%Y-%m-%d")
Built by: $(git config user.name) <$(git config user.email)>
Commit: $(git rev-parse --short HEAD 2>/dev/null || echo "not in git")
EOF

echo ""
echo "✓ All platforms built successfully!"
echo ""
echo "Summary:"
echo "  macOS ARM64:   vendor/macos-arm64/    ($(ls vendor/macos-arm64/*.whl 2>/dev/null | wc -l | xargs) wheels)"
echo "  Linux x86_64:  vendor/linux-x86_64/   ($(ls vendor/linux-x86_64/*.whl 2>/dev/null | wc -l | xargs) wheels)"
echo ""
echo "Next steps:"
echo "  1. Verify: ls vendor/*/SHA256SUMS"
echo "  2. Test: ./install_offline.sh"
echo "  3. Commit: git add vendor/ && git commit -m 'vendor: add multi-platform wheels'"
