from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from flask_login import LoginManager
from flask_mail import Mail
from minibase.config import Config

# (DATABASE) Definition
db = SQLAlchemy()

# (PASSWORD) Hashign Program to save paswords safely
bcrypt = Bcrypt()

# (LOGIN) Login manage plugin configuration
login_manager = LoginManager()
login_manager.login_view = 'users.login'
login_manager.login_message_category = 'info'  # Boostrap Info Message

# (EMAIL AGENT) Definition
mail = Mail()


def create_minibase(config_class=Config):
    # (FLASK) Main Flask Application
    app = Flask(__name__)
    app.config.from_object(Config)
    # (DATABASE) Initialisation
    db.init_app(app)
    # (PASSWORD) Initialisation
    bcrypt.init_app(app)
    # (LOGIN) Initialisation
    login_manager.init_app(app)
    # (EMAIL AGENT) Initialisation
    mail.init_app(app)
    # (FLASK) Importing adn then registering blueprints
    from minibase.users.routes import users
    from minibase.posts.routes import posts
    from minibase.main.routes import main
    from minibase.errors.handlers import errors
    app.register_blueprint(users)
    app.register_blueprint(posts)
    app.register_blueprint(main)
    app.register_blueprint(errors)
    # Retunr The ccreated app
    return app
