from flask import Blueprint, render_template, request, redirect
from ..models.massage import Massage
from ..extensions import db

me_liza = Blueprint('me_liza', __name__)

@me_liza.route('/chat_me_liza', methods=['POST', 'GET'])
def chat_me_liza():
    if request.method == "POST":
        mess = request.form['mess']
        if len(mess)>0:
            message = Massage( massage = mess, author_of_massage = 'me')

            try:
                db.session.add(message)
                db.session.commit()
                
                return redirect('/chat_me_liza')
            except Exception as e:
                print(str(e))
        else: 
            messages = Massage.query.all()
            last_message = messages[-1].massage
        return render_template('chats/meliza.html', messages = messages, last_message=last_message)
        
    else:
        messages = Massage.query.all()
        last_message = messages[-1].massage
        return render_template('chats/meliza.html', messages = messages, last_message=last_message)