# Path: hashview/models.py
# class Customers(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(40), nullable=False)
# 
# 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 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)
# 
# class HashfileHashes(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     hash_id = db.Column(db.Integer, nullable=False, index=True)
#     username = db.Column(db.String(256), nullable=True, default=None, index=True)
#     hashfile_id = db.Column(db.Integer, 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 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
# 
# Path: hashview/customers/forms.py
# class CustomersForm(FlaskForm):
#     name = StringField('Customer Name', validators=[DataRequired()])
# 
#     def validate_name(self, name):
#         customer = Customers.query.filter_by(name = name.data).first()
#         if customer:
#             raise ValidationError('That customer already exists. Please choose a different one.')
# 
# Path: hashview.py
# def data_retention_cleanup():


from flask import Blueprint, render_template, redirect, url_for, flash
from flask_login import login_required, current_user
from hashview.models import Customers, Jobs, Hashfiles, HashfileHashes, Hashes, HashNotifications
from hashview.customers.forms import CustomersForm
from hashview import db

customers = Blueprint('customers', __name__)

#############################################
# Customers
#############################################

@customers.route("/customers", methods=['GET'])
@login_required
def customers_list():
    customers = Customers.query.order_by(Customers.name).all()
    jobs = Jobs.query.all()
    hashfiles = Hashfiles.query.all()
    return render_template('customers.html', title='Cusomters', customers=customers, jobs=jobs, hashfiles=hashfiles)

@customers.route("/customers/add", methods=['GET', 'POST'])
@login_required
def customers_add():
    form = CustomersForm()
    if form.validate_on_submit():

# Path: hashview/models.py
# class Customers(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(40), nullable=False)
# 
# 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 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)
# 
# class HashfileHashes(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     hash_id = db.Column(db.Integer, nullable=False, index=True)
#     username = db.Column(db.String(256), nullable=True, default=None, index=True)
#     hashfile_id = db.Column(db.Integer, 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 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
# 
# Path: hashview/customers/forms.py
# class CustomersForm(FlaskForm):
#     name = StringField('Customer Name', validators=[DataRequired()])
# 
#     def validate_name(self, name):
#         customer = Customers.query.filter_by(name = name.data).first()
#         if customer:
#             raise ValidationError('That customer already exists. Please choose a different one.')
# 
# Path: hashview.py
# def data_retention_cleanup():


# Path: hashview/customers/routes.py
from flask import Blueprint, render_template, redirect, url_for, flash
from flask_login import login_required, current_user
from hashview.models import Customers, Jobs, Hashfiles, HashfileHashes, Hashes, HashNotifications
from hashview.customers.forms import CustomersForm
from hashview import db


customers = Blueprint('customers', __name__)

#############################################
# Customers
#############################################

@customers.route("/customers", methods=['GET'])
@login_required
def customers_list():
    customers = Customers.query.order_by(Customers.name).all()
    jobs = Jobs.query.all()
    hashfiles = Hashfiles.query.all()
    return render_template('customers.html', title='Cusomters', customers=customers, jobs=jobs, hashfiles=hashfiles)

@customers.route("/customers/add", methods=['GET', 'POST'])
@login_required
def customers_add():
    form = CustomersForm()
    if form.validate_on_submit():

        customer = Customers(name=form.name.data)