#!/bin/bash
# Quick test script to verify Python scraper setup

set -e

echo "🔧 CarnTrack Python Scraper - Setup Test"
echo "========================================="
echo ""

# Check Python is installed
echo "✓ Checking Python 3..."
if ! command -v python3 &> /dev/null; then
    echo "❌ Python 3 not found!"
    echo "   Install from: https://www.python.org/downloads/"
    exit 1
fi
PYTHON_VERSION=$(python3 --version)
echo "  Found: $PYTHON_VERSION"
echo ""

# Check if pip is installed
echo "✓ Checking pip..."
if ! command -v pip3 &> /dev/null; then
    echo "❌ pip3 not found!"
    echo "   Run: python3 -m ensurepip --default-pip"
    exit 1
fi
PIP_VERSION=$(pip3 --version)
echo "  Found: $PIP_VERSION"
echo ""

# Install dependencies
echo "✓ Installing required packages..."
echo "  Installing: undetected-chromedriver selenium"
pip3 install -q undetected-chromedriver selenium 2>/dev/null || {
    echo "❌ Failed to install packages!"
    echo "   Try: pip3 install undetected-chromedriver selenium"
    exit 1
}
echo "  ✓ Packages installed successfully"
echo ""

# Check if script exists
echo "✓ Checking scraper script..."
SCRIPT_PATH="scripts/shipping-scraper-python.py"
if [ ! -f "$SCRIPT_PATH" ]; then
    echo "❌ Script not found at: $SCRIPT_PATH"
    exit 1
fi
echo "  Found: $SCRIPT_PATH"
echo ""

# Quick validation of Python syntax
echo "✓ Validating Python syntax..."
python3 -m py_compile "$SCRIPT_PATH" || {
    echo "❌ Syntax error in script!"
    exit 1
}
echo "  ✓ Syntax valid"
echo ""

echo "═══════════════════════════════════════════"
echo "✅ Setup verification complete!"
echo ""
echo "Next steps:"
echo ""
echo "1️⃣  Test the scraper with a real container:"
echo "   python3 scripts/shipping-scraper-python.py MSKU1234567"
echo ""
echo "2️⃣  (Optional) Test with an API call:"
echo "   npm run dev"
echo "   curl -X POST http://localhost:3000/api/containers/scrape-eta \\"
echo "     -H 'Content-Type: application/json' \\"
echo "     -d '{\"container\": \"MSKU1234567\"}'"
echo ""
echo "3️⃣  Add to your dashboard:"
echo "   import { ScrapeETAButton } from '@/components/ScrapeETAButton';"
echo "   <ScrapeETAButton container={container.container_number} />"
echo ""
echo "📚 Full setup guide: SCRAPER_SETUP.md"
echo ""
