#!/usr/bin/env python3
from flask import Flask, render_template, request, jsonify
import yaml
import secrets
import string
import subprocess
from argon2 import PasswordHasher

app = Flask(__name__)

SERVICE_GROUPS = {
    'admins': [
        'traefik.ai-impress.com',
        'pgadmin.ai-impress.com',
        'rabbitmq.ai-impress.com',
        'n8n.ai-impress.com',
        'portainer.ai-impress.com',
        'status.ai-impress.com',
        'supabase.ai-impress.com',
        'wpp.ai-impress.com',
        'odoo.ai-impress.com'
    ],
    'developers': [
        'n8n.ai-impress.com',
        'portainer.ai-impress.com',
        'traefik.ai-impress.com',
        'status.ai-impress.com',
        'odoo.ai-impress.com'
    ],
    'analysts': [
        'pgadmin.ai-impress.com',
        'status.ai-impress.com',
        'odoo.ai-impress.com'
    ],
    'users': [
        'wiki.ai-impress.com',
        'status.ai-impress.com'
    ]
}

USERS_FILE = '/config/users_database.yml'

ph = PasswordHasher(
    time_cost=3,
    memory_cost=65536,
    parallelism=4,
    hash_len=32,
    salt_len=16
)

def generate_password(length=16):
    alphabet = string.ascii_letters + string.digits + "!@#$%^&*"
    return ''.join(secrets.choice(alphabet) for _ in range(length))

def hash_password(password):
    return ph.hash(password)

def restart_authelia():
    try:
        subprocess.run(['docker', 'restart', 'authelia'], check=True, capture_output=True)
        return True
    except Exception as e:
        print(f"Failed to restart authelia: {e}")
        return False

def load_users():
    with open(USERS_FILE, 'r') as f:
        return yaml.safe_load(f) or {'users': {}}

def save_users(data):
    with open(USERS_FILE, 'w') as f:
        yaml.dump(data, f, default_flow_style=False, sort_keys=False)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/api/groups')
def get_groups():
    return jsonify({
        'groups': list(SERVICE_GROUPS.keys()),
        'services': SERVICE_GROUPS
    })

@app.route('/api/users')
def get_users():
    data = load_users()
    users = []
    for username, info in data['users'].items():
        services = set()
        for group in info.get('groups', []):
            services.update(SERVICE_GROUPS.get(group, []))
        users.append({
            'username': username,
            'email': info.get('email', ''),
            'displayname': info.get('displayname', ''),
            'groups': info.get('groups', []),
            'services': sorted(services)
        })
    return jsonify(users)

@app.route('/api/users', methods=['POST'])
def add_user():
    try:
        data = request.json
        username = data.get('username')
        email = data.get('email')
        displayname = data.get('displayname')
        groups = data.get('groups', [])

        users_data = load_users()

        if username in users_data['users']:
            return jsonify({'error': 'User already exists'}), 400

        password = generate_password()
        password_hash = hash_password(password)

        users_data['users'][username] = {
            'displayname': displayname,
            'password': password_hash,
            'email': email,
            'groups': groups
        }

        save_users(users_data)
        restart_authelia()

        return jsonify({
            'success': True,
            'username': username,
            'password': password
        })
    except Exception as e:
        return jsonify({'error': str(e)}), 500

@app.route('/api/users/<username>', methods=['DELETE'])
def delete_user(username):
    try:
        users_data = load_users()

        if username not in users_data['users']:
            return jsonify({'error': 'User not found'}), 404

        del users_data['users'][username]
        save_users(users_data)
        restart_authelia()

        return jsonify({'success': True})
    except Exception as e:
        return jsonify({'error': str(e)}), 500

@app.route('/api/users/<username>/groups', methods=['PUT'])
def update_groups(username):
    try:
        data = request.json
        groups = data.get('groups', [])

        users_data = load_users()

        if username not in users_data['users']:
            return jsonify({'error': 'User not found'}), 404

        users_data['users'][username]['groups'] = groups
        save_users(users_data)
        restart_authelia()

        return jsonify({'success': True})
    except Exception as e:
        return jsonify({'error': str(e)}), 500

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=False)
@app.route('/api/users/<username>/reset-password', methods=['POST'])
def reset_password(username):
    try:
        users_data = load_users()

        if username not in users_data['users']:
            return jsonify({'error': 'User not found'}), 404

        # Generate new password and hash it
        new_password = generate_password()
        password_hash = hash_password(new_password)

        # Update the password in the database
        users_data['users'][username]['password'] = password_hash
        save_users(users_data)
        restart_authelia()

        return jsonify({
            'success': True,
            'username': username,
            'password': new_password
        })
    except Exception as e:
        return jsonify({'error': str(e)}), 500
