from flask import Flask, render_template, redirect, url_for
import os

app = Flask(__name__)

# Define the path to the users directory
USERS_DIR = "/var/www/html/finance/users"

@app.route('/')
def index():
    # Get list of users from the USERS_DIR
    users = [user.replace('_', ' ').title() for user in os.listdir(USERS_DIR) if os.path.isdir(os.path.join(USERS_DIR, user))]
    
    # Render the HTML template with the list of users
    return render_template('index.html', users=users)

@app.route('/users/<username>/open/')
def open_bills(username):
    # Redirect to the appropriate open bills URL for the user
    user_slug = username.replace(' ', '_').lower()
    return redirect(f'https://old.kalberer.dev/finance/users/{user_slug}/open/')

@app.route('/users/<username>/closed/')
def closed_bills(username):
    # Redirect to the appropriate closed bills URL for the user
    user_slug = username.replace(' ', '_').lower()
    return redirect(f'https://old.kalberer.dev/finance/users/{user_slug}/closed/')

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