# Path: hashview/models.py
# class JobNotifications(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     owner_id = db.Column(db.Integer, nullable=False)
#     job_id = db.Column(db.Integer, nullable=False)
#     method = db.Column(db.String(6), nullable=False)    # email, push
# 
# class HashNotifications(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     owner_id = db.Column(db.Integer, nullable=False)
#     hash_id = db.Column(db.Integer, nullable=False)
#     method = db.Column(db.String(6), nullable=False)    # email, push
# 
# class Jobs(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(50), nullable=False)
#     created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
#     updated_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
#     status = db.Column(db.String(20), nullable=False)           # Running, Paused, Completed, Queued, Canceled, Ready, Incomplete
#     started_at = db.Column(db.DateTime, nullable=True)          # These defaults should be changed
#     ended_at = db.Column(db.DateTime, nullable=True)            # These defaults should be changed
#     hashfile_id = db.Column(db.Integer, nullable=True)
#     customer_id = db.Column(db.Integer, db.ForeignKey('customers.id'), nullable=False)
#     owner_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
# 
# class Hashes(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     sub_ciphertext = db.Column(db.String(32), nullable=False, index=True)
#     ciphertext = db.Column(db.String(16383), nullable=False) # Setting this to max value for now. If we run into this being a limitation in the future we can revisit changing thist to TEXT or BLOB. https://sheeri.org/max-varchar-size/
#     hash_type = db.Column(db.Integer, nullable=False, index=True)
#     cracked = db.Column(db.Boolean, nullable=False)
#     plaintext = db.Column(db.String(256), index=True)
# 
# class Hashfiles(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(256), nullable=False)        # can probably be reduced
#     uploaded_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
#     runtime = db.Column(db.Integer, default=0)
#     customer_id = db.Column(db.Integer, nullable=False)
#     owner_id = db.Column(db.Integer, nullable=False)
# 
# Path: hashview.py
# def data_retention_cleanup():


from flask import Blueprint, render_template, redirect, flash, url_for
from flask_login import login_required, current_user
from hashview.models import JobNotifications, HashNotifications, Jobs, Hashes, Hashfiles
from hashview import db


notifications = Blueprint('notifications', __name__)


@notifications.route("/notifications", methods=['GET', 'POST'])
@login_required
def notifications_list():
    job_notifications = JobNotifications.query.filter_by(owner_id=current_user.id).all()
    hash_notifications = HashNotifications.query.filter_by(owner_id=current_user.id).all()
    hashfiles = Hashfiles.query.all()
    jobs = Jobs.query.all()
    hashes = db.session.query(Hashes).join(HashNotifications, Hashes.id == HashNotifications.hash_id).all()

    return render_template('notifications.html', title='Notifications', job_notifications=job_notifications, hash_notifications=hash_notifications, jobs=jobs, hashes=hashes, hashfiles=hashfiles)


@notifications.route("/notifications/delete/job/<int:notification_id>", methods=['GET'])
@login_required
def notifications_job_delete(notification_id):
    notification = JobNotifications.query.get(notification_id)
    if current_user.admin or notification.owner_id == current_user.id:
        db.session.delete(notification)

# Path: hashview/models.py
# class JobNotifications(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     owner_id = db.Column(db.Integer, nullable=False)
#     job_id = db.Column(db.Integer, nullable=False)
#     method = db.Column(db.String(6), nullable=False)    # email, push
# 
# class HashNotifications(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     owner_id = db.Column(db.Integer, nullable=False)
#     hash_id = db.Column(db.Integer, nullable=False)
#     method = db.Column(db.String(6), nullable=False)    # email, push
# 
# class Jobs(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(50), nullable=False)
#     created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
#     updated_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
#     status = db.Column(db.String(20), nullable=False)           # Running, Paused, Completed, Queued, Canceled, Ready, Incomplete
#     started_at = db.Column(db.DateTime, nullable=True)          # These defaults should be changed
#     ended_at = db.Column(db.DateTime, nullable=True)            # These defaults should be changed
#     hashfile_id = db.Column(db.Integer, nullable=True)
#     customer_id = db.Column(db.Integer, db.ForeignKey('customers.id'), nullable=False)
#     owner_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
# 
# class Hashes(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     sub_ciphertext = db.Column(db.String(32), nullable=False, index=True)
#     ciphertext = db.Column(db.String(16383), nullable=False) # Setting this to max value for now. If we run into this being a limitation in the future we can revisit changing thist to TEXT or BLOB. https://sheeri.org/max-varchar-size/
#     hash_type = db.Column(db.Integer, nullable=False, index=True)
#     cracked = db.Column(db.Boolean, nullable=False)
#     plaintext = db.Column(db.String(256), index=True)
# 
# class Hashfiles(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(256), nullable=False)        # can probably be reduced
#     uploaded_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
#     runtime = db.Column(db.Integer, default=0)
#     customer_id = db.Column(db.Integer, nullable=False)
#     owner_id = db.Column(db.Integer, nullable=False)
# 
# Path: hashview.py
# def data_retention_cleanup():


# Path: hashview/notifications/routes.py
from flask import Blueprint, render_template, redirect, flash, url_for
from flask_login import login_required, current_user
from hashview.models import JobNotifications, HashNotifications, Jobs, Hashes, Hashfiles
from hashview import db



notifications = Blueprint('notifications', __name__)


@notifications.route("/notifications", methods=['GET', 'POST'])
@login_required
def notifications_list():
    job_notifications = JobNotifications.query.filter_by(owner_id=current_user.id).all()
    hash_notifications = HashNotifications.query.filter_by(owner_id=current_user.id).all()
    hashfiles = Hashfiles.query.all()
    jobs = Jobs.query.all()
    hashes = db.session.query(Hashes).join(HashNotifications, Hashes.id == HashNotifications.hash_id).all()

    return render_template('notifications.html', title='Notifications', job_notifications=job_notifications, hash_notifications=hash_notifications, jobs=jobs, hashes=hashes, hashfiles=hashfiles)


@notifications.route("/notifications/delete/job/<int:notification_id>", methods=['GET'])
@login_required
def notifications_job_delete(notification_id):
    notification = JobNotifications.query.get(notification_id)
    if current_user.admin or notification.owner_id == current_user.id:
        db.session.delete(notification)

        db.session.commit()