#!/bin/bash
#
# Odds API Suite Installation Script
# For Windows, macOS (M1/Intel), and Linux
#
# Usage: bash install-odds-api-suite.sh
#

set -e  # Exit on error

echo "🎰 Odds API Suite Installer"
echo "============================"
echo ""

# Check for Python 3.8+
if ! command -v python3 &> /dev/null; then
    echo "❌ Error: Python 3 is not installed"
    echo "   Install from: https://www.python.org/downloads/"
    exit 1
fi

PYTHON_VERSION=$(python3 -c 'import sys; print(".".join(map(str, sys.version_info[:2])))')
echo "✅ Found Python $PYTHON_VERSION"
echo ""

# Download the package
DOWNLOAD_URL="https://sweeps.bankbonimus.com/assets/downloads/odds-api-suite.tar.gz"
TEMP_DIR=$(mktemp -d)
PACKAGE_FILE="$TEMP_DIR/odds-api-suite.tar.gz"

echo "📥 Downloading Odds API Suite..."
if command -v curl &> /dev/null; then
    curl -L -o "$PACKAGE_FILE" "$DOWNLOAD_URL" || {
        echo "❌ Download failed"
        exit 1
    }
elif command -v wget &> /dev/null; then
    wget -O "$PACKAGE_FILE" "$DOWNLOAD_URL" || {
        echo "❌ Download failed"
        exit 1
    }
else
    echo "❌ Error: Neither curl nor wget found"
    exit 1
fi

echo "✅ Download complete"
echo ""

# Extract the package
echo "📦 Extracting package..."
cd "$TEMP_DIR"
tar -xzf odds-api-suite.tar.gz || {
    echo "❌ Extraction failed"
    exit 1
}

echo "✅ Extraction complete"
echo ""

# Install the package
echo "🔧 Installing Odds API Suite..."
python3 setup.py install --user || {
    echo "❌ Installation failed"
    echo "   Try: pip3 install ."
    exit 1
}

echo "✅ Installation complete!"
echo ""

# Cleanup
cd /
rm -rf "$TEMP_DIR"

# Test installation
echo "🧪 Testing installation..."
if command -v oa &> /dev/null; then
    echo "✅ 'oa' command is available!"
else
    echo "⚠️  'oa' command not found in PATH"
    echo "   You may need to add ~/.local/bin to your PATH"
    echo "   Add this line to your ~/.zshrc or ~/.bash_profile:"
    echo "   export PATH=\"\$HOME/.local/bin:\$PATH\""
fi

echo ""
echo "🔑 IMPORTANT: Set up your API key"
echo "=================================="
echo ""
echo "1. Get your FREE API key:"
echo "   → Visit: https://the-odds-api.com/"
echo "   → Sign up (takes 30 seconds)"
echo "   → Copy your API key from the dashboard"
echo ""
echo "2. Set up your API key (choose one method):"
echo ""
echo "   Method 1 - Environment Variable (recommended):"
echo "   export ODDS_API_KEY=\"your-actual-api-key-here\""
echo ""
echo "   Method 2 - Config File:"
echo "   echo \"your-actual-api-key-here\" > ~/.odds-api-keys"
echo ""
echo "3. Test your setup:"
echo "   oa api-status"
echo ""
echo "🎉 Odds API Suite is ready!"
echo ""
echo "Next steps:"
echo "  1. Get your API key at: https://the-odds-api.com/"
echo "  2. Set up your key (see above)"
echo "  3. Run: oa prizepicks       # Find arbitrage opportunities"
echo "  4. Run: oa hedge            # Find hedge betting opportunities"
echo "  5. Run: oa odds             # View live odds"
echo ""
echo "For help: oa --help"
echo ""
