#!/bin/bash
set -e

# Detect platform
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
    PLATFORM="linux-x86_64"
elif [[ "$OSTYPE" == "darwin"* ]]; then
    ARCH=$(uname -m)
    if [[ "$ARCH" == "arm64" ]]; then
        PLATFORM="macos-arm64"
    else
        echo "ERROR: Only macOS ARM64 is supported"
        exit 1
    fi
else
    echo "ERROR: Unsupported platform: $OSTYPE"
    exit 1
fi

echo "Detected platform: $PLATFORM"

# Check if vendor directory exists
if [ ! -d "vendor/$PLATFORM" ]; then
    echo "ERROR: vendor/$PLATFORM not found!"
    echo "Run ./build-vendors-simple.sh to generate wheels"
    exit 1
fi

# Check for Python 3.12
if ! command -v python3.12 &> /dev/null; then
    echo "ERROR: Python 3.12 not found!"
    echo "Please install Python 3.12:"
    echo "  macOS: brew install python@3.12"
    echo "  Linux: apt-get install python3.12 python3.12-venv"
    exit 1
fi

# Check if already in a venv
if [ -n "$VIRTUAL_ENV" ]; then
    CURRENT_PYTHON=$(python --version 2>&1 | grep -oE '3\.[0-9]+')
    if [[ "$CURRENT_PYTHON" != "3.12" ]]; then
        echo "WARNING: You are in a venv with Python $CURRENT_PYTHON"
        echo "Deactivating and creating new Python 3.12 venv..."
        deactivate || true
    else
        echo "Already in Python 3.12 venv, using it..."
    fi
fi

# Create or use existing venv
VENV_DIR=".venv"
if [ ! -d "$VENV_DIR" ] || [ ! -f "$VENV_DIR/bin/python3.12" ]; then
    echo "Creating Python 3.12 virtual environment..."
    python3.12 -m venv "$VENV_DIR"
    echo "✓ Virtual environment created: $VENV_DIR"
else
    echo "Using existing venv: $VENV_DIR"
fi

# Activate venv
source "$VENV_DIR/bin/activate"

# Verify Python version
PYTHON_VERSION=$(python --version 2>&1 | grep -oE '3\.[0-9]+')
echo "Using Python $PYTHON_VERSION from: $(which python)"

if [[ "$PYTHON_VERSION" != "3.12" ]]; then
    echo "ERROR: Expected Python 3.12, got $PYTHON_VERSION"
    exit 1
fi

# Verify checksums
if [ -f "vendor/$PLATFORM/SHA256SUMS" ]; then
    echo ""
    echo "Verifying checksums..."
    (cd vendor/$PLATFORM && shasum -a 256 -c SHA256SUMS --quiet) || {
        echo "ERROR: Checksum verification failed!"
        exit 1
    }
    echo "✓ Checksums verified"
fi

# Install
echo ""
echo "Installing from vendor/$PLATFORM..."
pip install --upgrade pip --quiet
pip install --no-index --find-links=vendor/$PLATFORM -r requirements.txt

# Test
echo ""
echo "Running tests..."
python -m pytest -v tests/test_vectors.py || {
    echo "ERROR: Tests failed!"
    exit 1
}

echo ""
echo "Running built-in smoke test..."
python src/pyhdwallet.py test || {
    echo "ERROR: Smoke test failed!"
    exit 1
}

echo ""
echo "=========================================="
echo "✓ Installation complete and verified!"
echo "=========================================="
echo ""
echo "Virtual environment: $VENV_DIR"
echo "Python version: $(python --version)"
echo ""
echo "To activate this environment:"
echo "  source $VENV_DIR/bin/activate"
echo ""
echo "Generate a wallet with:"
echo "  python src/pyhdwallet.py gen --off-screen --file"
echo ""
