from flask import Flask, request, make_response, Response, render_template, redirect, url_for, send_from_directory, jsonify
import pandas as pd
import os 
import uuid
app = Flask(__name__, template_folder='templates')


@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'GET':
        return render_template('index.html')
    elif request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')

        if username == 'kynsight' and password == 'pass':
            return 'Success'
        else:
            return 'Failure'


@app.route('/file_upload', methods=['GET', 'POST'])
def file_upload():
    file = request.files.get('file')
    if file.content_type == 'text/plain':
        return file.read().decode()
    elif file.content_type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' or file.content_type == 'application/vnd.ms-excel':
        df = pd.read_excel(file)
        return df.to_html()

@app.route('/convert_csv', methods=['GET', 'POST'])
def convert_csv():
    file = request.files.get('file')
    df = pd.read_excel(file)
    response = Response(
            df.to_csv(),
            mimetype = 'text/csv',
            headers={'Content-Disposition': 'attachement; filename=result.csv'}
            )
    return response

@app.route('/convert_csv_two', methods=['GET', 'POST'])
def convert_csv_two():
    file = request.files.get('file')
    df = pd.read_excel(file)

    if not os.path.exists('downloads'):
        os.makedirs('downloads')

    filename = f'{uuid.uuid4()}.cvs'
    df.to_csv(os.path.join('downloads', filename))

    return render_template('download.html', filename=filename)

@app.route('/download/<filename>', methods=['GET', 'POST'])
def download(filename):
    return send_from_directory('downloads', filename, download_name='result.csv')



@app.route('/handle_post', methods=['GET', 'POST'])
def handle_post(): 
    greeting = request.json.get('greeting')
    name = request.json.get('name')
    
    with open('file.txt','w') as f:
        f.write(f'{greeting}, {name}')

    return jsonify({'message': 'Successfully written!'})


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)
