from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager
from flask_mail import Mail
from flask_wtf.csrf import CSRFProtect
from flask_crontab import Crontab
from config import FlaskApplicationSettings, V5Config

app = Flask(__name__)
app.config.from_object(FlaskApplicationSettings)

# Check security of secret
if FlaskApplicationSettings.SECRET_KEY == "a-random-secret-key":
    raise Exception("Please use a strong secret key!")

db = SQLAlchemy(app)
migrate = Migrate(app, db)
mail = Mail(app)
csrf = CSRFProtect(app)
crontab = Crontab(app)

login = LoginManager(app)
login.login_view = 'login'
login.login_message = "Veuillez vous authentifier avant de continuer."


# Register converters (needed for routing)
from app.utils.converters import *
app.url_map.converters['forum'] = ForumConverter
app.url_map.converters['topicpage'] = TopicPageConverter
app.url_map.converters['programpage'] = ProgramPageConverter

# Register routes
from app import routes

# Register filters
from app.utils import filters

# Register processors
from app import processors

# Register scheduled jobs
from app import jobs

# Enable flask-debug-toolbar if requested
if V5Config.ENABLE_FLASK_DEBUG_TOOLBAR:
    from flask_debugtoolbar import DebugToolbarExtension
    app.config['DEBUG_TB_PROFILER_ENABLED'] = True
    toolbar = DebugToolbarExtension(app)
