import requests
import json

API_BASE_URL = 'http://localhost:6000/api'

def test_parser_api():
    """Тест API парсера"""
    print("🧪 Тестирование API парсера...")
    
    # 1. Получаем статистику до
    response = requests.get(f'{API_BASE_URL}/stats')
    stats_before = response.json()
    if stats_before['success']:
        print(f"📊 Товаров до: {stats_before['stats']['total_products']}")
    
    # 2. Запускаем парсинг
    parser_data = {
        "queries": [
            "solar panel camping",
            "rv battery lithium",
            "portable camping gear"
        ],
        "max_pages": 1
    }
    
    response = requests.post(f'{API_BASE_URL}/parser/start', json=parser_data)
    result = response.json()
    
    if result['success']:
        print(f"✅ Парсинг запущен: {result['message']}")
        
        # 3. Ждем 10 секунд
        import time
        print("⏳ Ожидание завершения парсинга...")
        time.sleep(10)
        
        # 4. Проверяем статистику после
        response = requests.get(f'{API_BASE_URL}/stats')
        stats_after = response.json()
        if stats_after['success']:
            print(f"📊 Товаров после: {stats_after['stats']['total_products']}")
            diff = stats_after['stats']['total_products'] - stats_before['stats']['total_products']
            print(f"🆕 Добавлено товаров: {diff}")
    else:
        print(f"❌ Ошибка запуска парсинга: {result['error']}")

if __name__ == "__main__":
    test_parser_api()
