from flask import Flask
from config import Config                 # Importing the configuration
from auth import auth                     # Importing authentication setup
from routes import setup_routes           # Importing route setup
from logging_config import setup_logging  # Importing logging setup
from redis_config import setup_redis      # Import setup_redis
from limiter_config import setup_limiter  # Import limiter
from ui.ui import setup_ui                # Import setup_ui
import ssl

# Initialize Flask app
app = Flask(__name__)
app.config.from_object(Config)

# Set up logging
setup_logging(app)

# Set up Redis and get the connection
redis_connection = setup_redis(app)
if not redis_connection:
    raise RuntimeError("Failed to connect to Redis")

# Setup limiter
limiter = setup_limiter(app)

# Setup authentication and routes
setup_routes(limiter, app, redis_connection)

# Setup the UI
setup_ui(limiter, app)

if __name__ == '__main__':
    # SSL context setup
    context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
    context.load_cert_chain('certs/cert.crt', 'certs/cert.key')
    app.run(host='0.0.0.0', port=51820, ssl_context=context)
