import json
from telegram import Update, Bot
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from asgiref.sync import sync_to_async
from .models import TelegramBot
from pxy_langchain.services import LangchainAIService
from .handlers import dream_city_command, start, help_command, handle_location
import logging

logger = logging.getLogger(__name__)

@csrf_exempt
async def telegram_webhook(request, bot_name):
    """
    Webhook view that handles Telegram updates asynchronously and only uses LangChain.
    """
    try:
        logger.info(f"Webhook called for bot: {bot_name}")

        # Step 1: Fetch the bot instance asynchronously
        try:
            bot_instance = await sync_to_async(TelegramBot.objects.get)(name=bot_name, is_active=True)
            logger.info(f"Loaded bot configuration: {bot_instance}")
        except TelegramBot.DoesNotExist:
            logger.error(f"Bot '{bot_name}' not found or inactive.")
            return JsonResponse({"error": f"Bot '{bot_name}' not found."}, status=400)

        # Step 2: Ensure the bot has a LangChain assistant
        if not bot_instance.assistant:
            logger.error(f"No assistant configured for bot '{bot_name}'.")
            return JsonResponse({"error": "Assistant not configured."}, status=400)

        # Step 3: Process POST request from Telegram
        if request.method == "POST":
            try:
                request_body = json.loads(request.body.decode("utf-8"))
                update = Update.de_json(request_body, Bot(token=bot_instance.token))
                logger.info(f"Update received: {update}")
            except json.JSONDecodeError as e:
                logger.error(f"Failed to decode JSON: {e}")
                return JsonResponse({"error": "Invalid JSON payload"}, status=400)

            # Step 4: Route commands to the appropriate handlers
            if update.message:
                if update.message.text == "/start":
                    await start(update)
                elif update.message.text == "/help":
                    await help_command(update)
                elif update.message.text == "/dream_city":
                    await dream_city_command(update)
                elif update.message.location:
                    await handle_location(update)
                else:
                    # Step 5: Process AI-generated response using LangChain
                    assistant_instance = await sync_to_async(LangchainAIService)(bot_instance.assistant)
                    bot_response = await sync_to_async(assistant_instance.generate_response)(update.message.text)

                    # Step 6: Send the response back to Telegram
                    await update.message.reply_text(bot_response)

            return JsonResponse({"status": "ok"})

        logger.warning("Received non-POST request")
        return JsonResponse({"error": "Invalid request method"}, status=400)

    except Exception as e:
        logger.error(f"Error in webhook: {e}")
        return JsonResponse({"error": f"Unexpected error: {str(e)}"}, status=500)
