# Dependencies
from aurora import Controller, View, Forms
from models import Users, Notes
from aurora.security import login_required, get_session
from flask import request
from datetime import datetime

# The controller class
class NewNote(Controller):

    # POST Method
    @login_required(app='users')
    def post(self):
        # The required models
        user = Users().read(where={'username':get_session('user')}).first()
        notes = Notes()

        # Form data
        data = request.form
        form = Forms(data)

        # Valid form data
        if form.validate():
            # Collect form inputs
            title = data.get('title')
            content = data.get('content')

            # Required fields
            if not title or not content:
                return {
                    'error': '<i class="fas fa-exclamation-triangle mr-1"></i> Form data is invalid!',
                }, 400

            # Everything is fine
            # Insert new note into the database
            data = {
                'user_id': user['id'],
                'title': title,
                'content': content,
                # 'date': datetime.now().strftime("%m-%d-%Y")
            }
            notes.create(data=data)

            # Return the result
            return {
                'success': '<i class="fas fa-check-circle mr-1"></i> The new note created successfully!',
            }, 200

        # Invalid form data
        else:
            # Return the result
            return {
                'error': '<i class="fas fa-exclamation-triangle mr-1"></i> Form data is invalid!',
            }, 400


    # GET Method
    @login_required(app='users')
    def get(self):
        # The required models
        user = Users().read(where={'username':get_session('user')}).first()
        notes = Notes().read(where={'user_id':user['id']}, order_by={'id':'DESC'}).all()

        form = Forms()
        
        return View('create', user=user, form=form)


[((12, 5, 12, 32), 'aurora.security.login_required', 'login_required', (), '', False, 'from aurora.security import login_required, get_session\n'), ((58, 5, 58, 32), 'aurora.security.login_required', 'login_required', (), '', False, 'from aurora.security import login_required, get_session\n'), ((16, 16, 16, 23), 'models.Notes', 'Notes', ({}, {}), '()', False, 'from models import Users, Notes\n'), ((20, 15, 20, 26), 'aurora.Forms', 'Forms', ({(20, 21, 20, 25): 'data'}, {}), '(data)', False, 'from aurora import Controller, View, Forms\n'), ((64, 15, 64, 22), 'aurora.Forms', 'Forms', ({}, {}), '()', False, 'from aurora import Controller, View, Forms\n'), ((66, 15, 66, 51), 'aurora.View', 'View', (), '', False, 'from aurora import Controller, View, Forms\n'), ((15, 15, 15, 22), 'models.Users', 'Users', ({}, {}), '()', False, 'from models import Users, Notes\n'), ((61, 15, 61, 22), 'models.Users', 'Users', ({}, {}), '()', False, 'from models import Users, Notes\n'), ((62, 16, 62, 23), 'models.Notes', 'Notes', ({}, {}), '()', False, 'from models import Users, Notes\n'), ((15, 46, 15, 65), 'aurora.security.get_session', 'get_session', ({(15, 58, 15, 64): '"""user"""'}, {}), "('user')", False, 'from aurora.security import login_required, get_session\n'), ((61, 46, 61, 65), 'aurora.security.get_session', 'get_session', ({(61, 58, 61, 64): '"""user"""'}, {}), "('user')", False, 'from aurora.security import login_required, get_session\n')]