#!/usr/bin/env python3
"""
Скрипт для тестирования Telegram бота локально
"""

import asyncio
import httpx
import logging
from telegram_bot import MovieSearchBot

# Настройка логирования
logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    level=logging.INFO
)
logger = logging.getLogger(__name__)

async def test_api_connection():
    """Тестирование подключения к API"""
    try:
        # Тестируем подключение к основному API
        async with httpx.AsyncClient() as client:
            response = await client.get("http://localhost:8089/api/search/terminator")
            if response.status_code == 200:
                logger.info("✅ Main API connection successful")
                return True
            else:
                logger.error(f"❌ Main API error: {response.status_code}")
                return False
    except Exception as e:
        logger.error(f"❌ API connection failed: {e}")
        return False

async def test_tmdb_connection():
    """Тестирование подключения к TMDB"""
    try:
        async with httpx.AsyncClient() as client:
            response = await client.get(
                "https://api.themoviedb.org/3/search/movie",
                params={
                    "api_key": "6d58225585fb77af5945a964de41849f",
                    "query": "terminator",
                    "language": "ru-RU"
                }
            )
            if response.status_code == 200:
                logger.info("✅ TMDB API connection successful")
                return True
            else:
                logger.error(f"❌ TMDB API error: {response.status_code}")
                return False
    except Exception as e:
        logger.error(f"❌ TMDB connection failed: {e}")
        return False

async def main():
    """Главная функция тестирования"""
    logger.info("🧪 Testing Telegram Bot components...")
    
    # Тестируем подключения
    api_ok = await test_api_connection()
    tmdb_ok = await test_tmdb_connection()
    
    if api_ok and tmdb_ok:
        logger.info("✅ All tests passed! Bot should work correctly.")
        logger.info("🚀 Starting Telegram Bot...")
        
        # Запускаем бота
        bot = MovieSearchBot()
        await bot.run()
    else:
        logger.error("❌ Some tests failed. Please check your setup.")
        logger.error("Make sure the main application is running on port 8089")

if __name__ == "__main__":
    asyncio.run(main())
