# save this as app.py
from flask import Flask
from flask import request
from werkzeug.utils import secure_filename
from flask_mail import Mail, Message

app = Flask(__name__)
app.config['MAIL_SERVER']='smtp-stax.alwaysdata.net'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USERNAME'] = 'backup@pokeranalytics.net'
app.config['MAIL_PASSWORD'] = 'StaxBackup****'
app.config['MAIL_USE_TLS'] = True

mail = Mail(app)

# EMAIL_HOST = 'smtp-stax.alwaysdata.net'
# EMAIL_PORT = 587
# EMAIL_HOST_USER = 'backup@pokeranalytics.net'
# EMAIL_HOST_PASSWORD = 'StaxBackup****'
# EMAIL_USE_TLS = True
# DEFAULT_FROM_EMAIL = 'Poker Analytics Backup <backup@pokeranalytics.net>'


@app.route('/')
def index():
    return 'Yeah! Install looks fine!'

@app.route('/send', methods=['GET', 'POST'])
def sender():
    if request.method == 'POST':
        # mail = request.args.get('mail', '')
        recipient = request.form['recipient']
        file = request.files['file']
        return send_mail(recipient, file)
    else:
        return show_method_error(request.method)

def send_mail(recipient, file):

    msg = Message('Poker Analytics Backup', sender = 'backup@pokeranalytics.net', recipients = [recipient])
    msg.body = "This is the backup"

    filename = secure_filename(file.filename)
    msg.attach(filename, "text/csv", file.read())
    mail.send(msg)

    print(mail)

    return f'POST to {recipient}'

def show_method_error(method):
    return f'bad method:{method}'
