from django.shortcuts import render, redirect
from .models import ChatMessage

def index(request):
    if request.method == 'POST':
        message = request.POST.get('message')
        if message:
            ChatMessage.objects.create(sender='user', message=message)
            # Canned response from the bot
            ChatMessage.objects.create(sender='bot', message='Thank you for your message. An agent will be with you shortly.')
        return redirect('index')

    messages = ChatMessage.objects.all().order_by('timestamp')
    return render(request, 'core/index.html', {'messages': messages})