import json
from django.http import JsonResponse, HttpResponse
from rest_framework.views import APIView
from django.views.decorators.csrf import csrf_exempt

class DownloadConversationView(APIView):
    @csrf_exempt
    def get(self, request, *args, **kwargs):
        try:
            conversation_history = request.session.get('conversation_history', [])
            response = HttpResponse(
                json.dumps(conversation_history),
                content_type='application/json'
            )
            response['Content-Disposition'] = 'attachment; filename=conversation.json'
            return response
        except Exception as e:
            logger.error(f"Erreur lors du téléchargement de la conversation : {str(e)}")
            return HttpResponse(f"<div class='message error'>Erreur lors du téléchargement de la conversation : {str(e)}</div>", status=500, content_type="text/html")
