# 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)


[((277, 304), 'aurora.security.login_required', 'login_required', ([], {'app': '"""users"""'}), "(app='users')\n", (291, 304), False, 'from aurora.security import login_required, get_session\n'), ((1662, 1689), 'aurora.security.login_required', 'login_required', ([], {'app': '"""users"""'}), "(app='users')\n", (1676, 1689), False, 'from aurora.security import login_required, get_session\n'), ((447, 454), 'models.Notes', 'Notes', ([], {}), '()\n', (452, 454), False, 'from models import Users, Notes\n'), ((519, 530), 'aurora.Forms', 'Forms', (['data'], {}), '(data)\n', (524, 530), False, 'from aurora import Controller, View, Forms\n'), ((1920, 1927), 'aurora.Forms', 'Forms', ([], {}), '()\n', (1925, 1927), False, 'from aurora import Controller, View, Forms\n'), ((1952, 1988), 'aurora.View', 'View', (['"""create"""'], {'user': 'user', 'form': 'form'}), "('create', user=user, form=form)\n", (1956, 1988), False, 'from aurora import Controller, View, Forms\n'), ((370, 377), 'models.Users', 'Users', ([], {}), '()\n', (375, 377), False, 'from models import Users, Notes\n'), ((1754, 1761), 'models.Users', 'Users', ([], {}), '()\n', (1759, 1761), False, 'from models import Users, Notes\n'), ((1831, 1838), 'models.Notes', 'Notes', ([], {}), '()\n', (1836, 1838), False, 'from models import Users, Notes\n'), ((401, 420), 'aurora.security.get_session', 'get_session', (['"""user"""'], {}), "('user')\n", (412, 420), False, 'from aurora.security import login_required, get_session\n'), ((1785, 1804), 'aurora.security.get_session', 'get_session', (['"""user"""'], {}), "('user')\n", (1796, 1804), False, 'from aurora.security import login_required, get_session\n')]