# Path: hashview/agents/forms.py
# class AgentsForm(FlaskForm):
#     name = StringField('Agent Name', validators=[DataRequired()])
#     id = StringField('agent_id', validators=[DataRequired()])
#     
#     submit = SubmitField('Update')
# 
# Path: hashview/models.py
# class Agents(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(100), nullable=False)         # can probably be reduced
#     src_ip = db.Column(db.String(15), nullable=False)
#     uuid = db.Column(db.String(60), nullable=False)          # can probably be reduced
#     status = db.Column(db.String(20), nullable=False)        # Pending, Syncing, Working, Idle
#     hc_status = db.Column(db.String(6000))
#     last_checkin = db.Column(db.DateTime)
#     benchmark = db.Column(db.String(20))
#     cpu_count = db.Column(db.Integer)
#     gpu_count = db.Column(db.Integer)
# 
# class JobTasks(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     job_id = db.Column(db.Integer, nullable=False)
#     task_id = db.Column(db.Integer, nullable=False)
#     command = db.Column(db.String(1024))                  
#     status = db.Column(db.String(50), nullable=False)       # Running, Paused, Not Started, Completed, Queued, Canceled, Importing
#     agent_id = db.Column(db.Integer, db.ForeignKey('agents.id'))
# 
# Path: hashview/utils/utils.py
# def getHashviewVersion():
#     with open('VERSION.TXT') as f:
#         return f.read().split('\n')[0]
# 
# Path: hashview.py
# def data_retention_cleanup():


from flask import Blueprint, render_template, abort, flash, redirect, url_for, send_from_directory
from flask_login import login_required, current_user
from hashview.agents.forms import AgentsForm
from hashview.models import Agents, JobTasks
from hashview.utils.utils import getHashviewVersion
from hashview import db
import os
@agents.route("/agents/edit/<int:agent_id>", methods=['GET', 'POST'])
@login_required
def agents_edit(agent_id):
    if current_user.admin:
        agentsForm = AgentsForm()

        if agentsForm.validate_on_submit():
            agent_name = agentsForm.name.data
            agent_id = agentsForm.id.data

            agent = Agents.query.get(agent_id)
            agent.name = agent_name
            db.session.commit()

            flash('Updated Agents Name', 'success')
            return redirect(url_for('agents.agents_list'))
        else:
            agent = Agents.query.get(agent_id)
            return render_template('agents_edit.html', title='agents', agent=agent, agentsForm=agentsForm)
    else:
        flash('You are unauthorized to edit agent data.', 'danger')
    redirect(url_for('agents.agents_list'))

@agents.route("/agents/<int:agent_id>/authorize", methods=['GET'])
@login_required
def agents_authorize(agent_id):
    if current_user.admin:
        agent = Agents.query.get(agent_id)

        agent.status = 'Authorized'

# Path: hashview/agents/forms.py
# class AgentsForm(FlaskForm):
#     name = StringField('Agent Name', validators=[DataRequired()])
#     id = StringField('agent_id', validators=[DataRequired()])
#     
#     submit = SubmitField('Update')
# 
# Path: hashview/models.py
# class Agents(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(100), nullable=False)         # can probably be reduced
#     src_ip = db.Column(db.String(15), nullable=False)
#     uuid = db.Column(db.String(60), nullable=False)          # can probably be reduced
#     status = db.Column(db.String(20), nullable=False)        # Pending, Syncing, Working, Idle
#     hc_status = db.Column(db.String(6000))
#     last_checkin = db.Column(db.DateTime)
#     benchmark = db.Column(db.String(20))
#     cpu_count = db.Column(db.Integer)
#     gpu_count = db.Column(db.Integer)
# 
# class JobTasks(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     job_id = db.Column(db.Integer, nullable=False)
#     task_id = db.Column(db.Integer, nullable=False)
#     command = db.Column(db.String(1024))                  
#     status = db.Column(db.String(50), nullable=False)       # Running, Paused, Not Started, Completed, Queued, Canceled, Importing
#     agent_id = db.Column(db.Integer, db.ForeignKey('agents.id'))
# 
# Path: hashview/utils/utils.py
# def getHashviewVersion():
#     with open('VERSION.TXT') as f:
#         return f.read().split('\n')[0]
# 
# Path: hashview.py
# def data_retention_cleanup():


# Path: hashview/agents/routes.py
from flask import Blueprint, render_template, abort, flash, redirect, url_for, send_from_directory
from flask_login import login_required, current_user
from hashview.agents.forms import AgentsForm
from hashview.models import Agents, JobTasks
from hashview.utils.utils import getHashviewVersion
from hashview import db
import os

@agents.route("/agents/edit/<int:agent_id>", methods=['GET', 'POST'])
@login_required
def agents_edit(agent_id):
    if current_user.admin:
        agentsForm = AgentsForm()

        if agentsForm.validate_on_submit():
            agent_name = agentsForm.name.data
            agent_id = agentsForm.id.data

            agent = Agents.query.get(agent_id)
            agent.name = agent_name
            db.session.commit()

            flash('Updated Agents Name', 'success')
            return redirect(url_for('agents.agents_list'))
        else:
            agent = Agents.query.get(agent_id)
            return render_template('agents_edit.html', title='agents', agent=agent, agentsForm=agentsForm)
    else:
        flash('You are unauthorized to edit agent data.', 'danger')
    redirect(url_for('agents.agents_list'))

@agents.route("/agents/<int:agent_id>/authorize", methods=['GET'])
@login_required
def agents_authorize(agent_id):
    if current_user.admin:
        agent = Agents.query.get(agent_id)

        agent.status = 'Authorized'

        db.session.commit()