from flask import Flask, render_template, request, send_from_directory
import httpx
import asyncio
from dotenv import load_dotenv
from . import ui_bp
import os

load_dotenv()  # Load environment variables from .env file

def get_devices():
    devices = []
    for key in os.environ:
        if key.endswith('_MAC'):
            device_name = key[:-4].lower()  # Get device name
            devices.append(device_name)
    return devices

def setup_ui(limiter, app):

    # Serve static files and exempt from rate limiting
    @app.route('/ui/static/<path:filename>')
    @limiter.exempt
    def serve_static(filename):
        static_dir = os.path.join(app.root_path, 'ui', 'static')
        return send_from_directory(static_dir, filename)

    @ui_bp.route('/')
    @limiter.exempt
    def home():
        devices = get_devices()
        return render_template('home.html', devices=devices)

    app.register_blueprint(ui_bp)
