# Path: hashview/searches/forms.py
# class SearchForm(FlaskForm):
#     search_type = SelectField('Search Type', choices=[('user', 'user'), ('hash', 'hash'), ('password', 'password')], validators=[DataRequired()])
#     query = StringField('', validators=([DataRequired()]))
#     submit = SubmitField('Search')  
# 
# 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 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)
# 
# Path: hashview.py
# def data_retention_cleanup():


from flask import Blueprint, render_template, redirect, url_for, request, flash
from flask_login import login_required
from hashview.searches.forms import SearchForm
from hashview.models import Customers, Hashfiles, HashfileHashes, Hashes
from hashview import db

searches = Blueprint('searches', __name__)

@searches.route("/search", methods=['GET', 'POST'])
@login_required
def searches_list():
    customers = Customers.query.all()
    hashfiles = Hashfiles.query.all()
    searchForm = SearchForm()
    # TODO
    # We should be able to include Customers and Hashfiles in the following queries
    if searchForm.validate_on_submit():
        if searchForm.search_type.data == 'hash':
            results = db.session.query(Hashes, HashfileHashes).join(HashfileHashes, Hashes.id==HashfileHashes.hash_id).filter(Hashes.ciphertext==searchForm.query.data).all()
        elif searchForm.search_type.data == 'user':

# Path: hashview/searches/forms.py
# class SearchForm(FlaskForm):
#     search_type = SelectField('Search Type', choices=[('user', 'user'), ('hash', 'hash'), ('password', 'password')], validators=[DataRequired()])
#     query = StringField('', validators=([DataRequired()]))
#     submit = SubmitField('Search')  
# 
# 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 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)
# 
# Path: hashview.py
# def data_retention_cleanup():


# Path: hashview/searches/routes.py
from flask import Blueprint, render_template, redirect, url_for, request, flash
from flask_login import login_required
from hashview.searches.forms import SearchForm
from hashview.models import Customers, Hashfiles, HashfileHashes, Hashes
from hashview import db


searches = Blueprint('searches', __name__)

@searches.route("/search", methods=['GET', 'POST'])
@login_required
def searches_list():
    customers = Customers.query.all()
    hashfiles = Hashfiles.query.all()
    searchForm = SearchForm()
    # TODO
    # We should be able to include Customers and Hashfiles in the following queries
    if searchForm.validate_on_submit():
        if searchForm.search_type.data == 'hash':
            results = db.session.query(Hashes, HashfileHashes).join(HashfileHashes, Hashes.id==HashfileHashes.hash_id).filter(Hashes.ciphertext==searchForm.query.data).all()
        elif searchForm.search_type.data == 'user':

            results = db.session.query(Hashes, HashfileHashes).join(HashfileHashes, Hashes.id==HashfileHashes.hash_id).filter(HashfileHashes.username.like('%' + searchForm.query.data.encode('latin-1').hex() + '%')).all()