#!/usr/bin/env python3
"""
Welcome to YaSID - The The Yasi System Installer Daemon!
"""

from flask import Flask, request, session, redirect, \
     url_for, render_template, flash, Blueprint
from waitress import serve
import logging
import dmm.lsblk as lsblk
import importlib
import yaml
import sys

# configuration
CONFIGFILE="/etc/yasi/yasi.yaml"

# import config
global config
config_contents = open(CONFIGFILE, "r")
config = yaml.safe_load(config_contents)
APPS = config['settings']['apps']
DEV_MODE = config['settings']['devmode']

# create our application in flask
app = Flask(__name__)
app.config.from_object(__name__)
app.config['CONFIG'] = config
app.app_context().push()
config['settings']['menu'] = {}
sys.path.append('/usr/share/yasi-daemon')

if DEV_MODE:
    print("Note: Starting in devmode!")
    DEBUG = True
    tracebacks = True

# Register blueprints here
for APP in APPS.split(" "):
    print(f"Loading applet: {APP} ")
    # Dynamically import the blueprint module
    module = importlib.import_module(f"yasi_applets.{APP}")
    # Get the blueprint (bp) from the imported module
    bp = getattr(module, "bp")
    # Register the blueprint with the app
    app.register_blueprint(bp)

# configure logs
# logging.basicConfig()
logger = logging.getLogger('waitress')
if DEV_MODE == "1":
    logger.setLevel(logging.DEBUG)
    logger.debug("logger set to DEBUG")

if __name__ == '__main__':
    serve(app,
    host='127.0.0.1',
    port=9274,
    url_scheme='http',
    expose_tracebacks=DEV_MODE)

