from project.utils import auth
from flask import Blueprint, request, redirect
from project import db, session
from project.models import Task
from project.utils import utils
from sqlalchemy import text
from datetime import datetime
import json

tasks = Blueprint('tasks', __name__)


@tasks.route('/retrieve', methods=['POST'])
@auth.login_required
def retrieve():
    """Swap to a post request because you are sending data"""
    # Support for the reverse query here
    todo_list = utils.retrieve_tasks_helper()

    # Must generate the initial task
    if len(todo_list) == 0:
        task = Task(
            content="Edit your first task",
            user_id=session['user_id'],
            due_date=None
        )
        db.session.add(task)
        db.session.commit()
        todo_list = utils.retrieve_tasks_helper()

    return json.dumps(todo_list)


@tasks.route('/retrieve_tasks', methods=['POST'])
@auth.login_required
def retrieve_tasks():
    user_id = session['user_id']

    # find all root nodes
    roots = []
    root1 = Task.query.filter(Task.user_id == user_id).filter(Task.lft == 0).first()