# Path: hashview/task_groups/forms.py
# class TaskGroupsForm(FlaskForm):
#     name = StringField('Name', validators=([DataRequired()]))
#     submit = SubmitField('Create')  
# 
#     def validate_task(self, name):
#         task = Tasks.query.filter_by(name = name.data).first()
#         if task:
#             raise ValidationError('That task name is taken. Please choose a different one.')
# 
# Path: hashview/models.py
# class Tasks(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(100), nullable=False)
#     hc_attackmode = db.Column(db.String(25), nullable=False) # dictionary, mask, bruteforce, combinator
#     owner_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
#     wl_id = db.Column(db.Integer)
#     rule_id = db.Column(db.Integer)
#     hc_mask = db.Column(db.String(50))
# 
# class TaskGroups(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(50), nullable=False)
#     owner_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
#     tasks = db.Column(db.String(256), nullable=False)
# 
# class Users(db.Model, UserMixin):
#     id = db.Column(db.Integer, primary_key=True)
#     first_name = db.Column(db.String(20), nullable=False)
#     last_name = db.Column(db.String(20), nullable=False)
#     email_address = db.Column(db.String(50), unique=True, nullable=False)
#     password = db.Column(db.String(60), nullable=False)
#     admin = db.Column(db.Boolean, nullable=False, default=False)
#     pushover_app_id = db.Column(db.String(50), nullable=True)
#     pushover_user_key = db.Column(db.String(50), nullable=True)
#     wordlists = db.relationship('Wordlists', backref='tbd', lazy=True)
#     rules = db.relationship('Rules', backref='owner', lazy=True)
#     jobs = db.relationship('Jobs', backref='owner', lazy=True)
#     tasks = db.relationship('Tasks', backref='owner', lazy=True)
#     taskgroups = db.relationship('TaskGroups', backref='owner', lazy=True)
# 
#     def get_reset_token(self, expires_sec=1800):
#         s = Serializer(Config.SECRET_KEY, expires_sec)
#         return s.dumps({'user_id': self.id}).decode('utf-8')
# 
#     @staticmethod
#     def verify_reset_token(token):
#         s = Serializer(Config.SECRET_KEY)
#         try:
#             user_id = s.loads(token)['user_id']
#         except:
#             return None     
#         return Users.query.get(user_id)
# 
# Path: hashview.py
# def data_retention_cleanup():


from flask import Blueprint, render_template, redirect, url_for, flash, abort
from flask_login import login_required, current_user
from hashview.task_groups.forms import TaskGroupsForm
from hashview.models import Tasks, TaskGroups, Users
from hashview import db
import json

@task_groups.route("/task_groups", methods=['GET', 'POST'])
@login_required
def task_groups_list():
    task_groups = TaskGroups.query.all()
    tasks = Tasks.query.all()
    users = Users.query.all()

    return render_template('task_groups.html', title='Task Groups', task_groups=task_groups, users=users, tasks=tasks) 

@task_groups.route("/task_groups/add", methods=['GET', 'POST'])
@login_required
def task_groups_add():
    taskGroupsForm = TaskGroupsForm()
    tasks = Tasks.query
    emptyList = []
    if taskGroupsForm.validate_on_submit():
        task_group = TaskGroups(name=taskGroupsForm.name.data, owner_id=current_user.id, tasks=str(emptyList))
        db.session.add(task_group)
        db.session.commit()
        flash(f'Task {taskGroupsForm.name.data} created!', 'success')           
        # TODO change this redirect to use a url_for
        #return redirect(url_for('taskgroups.taskgroups_assigntask', taskgroup_id=taskgroup.id))  
        return redirect("assigned_tasks/"+str(task_group.id))
    return render_template('task_groups_add.html', title='Tasks Add', tasks=tasks, taskGroupsForm=taskGroupsForm)   

@task_groups.route("/task_groups/assigned_tasks/<int:task_group_id>", methods=['GET', 'POST'])
@login_required
def task_groups_assigned_tasks(task_group_id):
    task_group = TaskGroups.query.get(task_group_id)

# Path: hashview/task_groups/forms.py
# class TaskGroupsForm(FlaskForm):
#     name = StringField('Name', validators=([DataRequired()]))
#     submit = SubmitField('Create')  
# 
#     def validate_task(self, name):
#         task = Tasks.query.filter_by(name = name.data).first()
#         if task:
#             raise ValidationError('That task name is taken. Please choose a different one.')
# 
# Path: hashview/models.py
# class Tasks(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(100), nullable=False)
#     hc_attackmode = db.Column(db.String(25), nullable=False) # dictionary, mask, bruteforce, combinator
#     owner_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
#     wl_id = db.Column(db.Integer)
#     rule_id = db.Column(db.Integer)
#     hc_mask = db.Column(db.String(50))
# 
# class TaskGroups(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(50), nullable=False)
#     owner_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
#     tasks = db.Column(db.String(256), nullable=False)
# 
# class Users(db.Model, UserMixin):
#     id = db.Column(db.Integer, primary_key=True)
#     first_name = db.Column(db.String(20), nullable=False)
#     last_name = db.Column(db.String(20), nullable=False)
#     email_address = db.Column(db.String(50), unique=True, nullable=False)
#     password = db.Column(db.String(60), nullable=False)
#     admin = db.Column(db.Boolean, nullable=False, default=False)
#     pushover_app_id = db.Column(db.String(50), nullable=True)
#     pushover_user_key = db.Column(db.String(50), nullable=True)
#     wordlists = db.relationship('Wordlists', backref='tbd', lazy=True)
#     rules = db.relationship('Rules', backref='owner', lazy=True)
#     jobs = db.relationship('Jobs', backref='owner', lazy=True)
#     tasks = db.relationship('Tasks', backref='owner', lazy=True)
#     taskgroups = db.relationship('TaskGroups', backref='owner', lazy=True)
# 
#     def get_reset_token(self, expires_sec=1800):
#         s = Serializer(Config.SECRET_KEY, expires_sec)
#         return s.dumps({'user_id': self.id}).decode('utf-8')
# 
#     @staticmethod
#     def verify_reset_token(token):
#         s = Serializer(Config.SECRET_KEY)
#         try:
#             user_id = s.loads(token)['user_id']
#         except:
#             return None     
#         return Users.query.get(user_id)
# 
# Path: hashview.py
# def data_retention_cleanup():


# Path: hashview/task_groups/routes.py
from flask import Blueprint, render_template, redirect, url_for, flash, abort
from flask_login import login_required, current_user
from hashview.task_groups.forms import TaskGroupsForm
from hashview.models import Tasks, TaskGroups, Users
from hashview import db
import json


@task_groups.route("/task_groups", methods=['GET', 'POST'])
@login_required
def task_groups_list():
    task_groups = TaskGroups.query.all()
    tasks = Tasks.query.all()
    users = Users.query.all()

    return render_template('task_groups.html', title='Task Groups', task_groups=task_groups, users=users, tasks=tasks) 

@task_groups.route("/task_groups/add", methods=['GET', 'POST'])
@login_required
def task_groups_add():
    taskGroupsForm = TaskGroupsForm()
    tasks = Tasks.query
    emptyList = []
    if taskGroupsForm.validate_on_submit():
        task_group = TaskGroups(name=taskGroupsForm.name.data, owner_id=current_user.id, tasks=str(emptyList))
        db.session.add(task_group)
        db.session.commit()
        flash(f'Task {taskGroupsForm.name.data} created!', 'success')           
        # TODO change this redirect to use a url_for
        #return redirect(url_for('taskgroups.taskgroups_assigntask', taskgroup_id=taskgroup.id))  
        return redirect("assigned_tasks/"+str(task_group.id))
    return render_template('task_groups_add.html', title='Tasks Add', tasks=tasks, taskGroupsForm=taskGroupsForm)   

@task_groups.route("/task_groups/assigned_tasks/<int:task_group_id>", methods=['GET', 'POST'])
@login_required
def task_groups_assigned_tasks(task_group_id):
    task_group = TaskGroups.query.get(task_group_id)

    tasks = Tasks.query