from flask import Flask, url_for
from flask_session import Session
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from flask_login import LoginManager
from flask_mail import Mail
from flask_migrate import Migrate
from minibase.config import AppConfig

# (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()
session = Session()


def create_app():
    app = Flask(__name__, template_folder='templates', static_folder='static', static_url_path='/')
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///./test.db'
    app.secret_key = 'SOME KEY'

    #app.config.from_object(AppConfig)
    # (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)
    # Session for variable manipulation on server side

    Migrate(app, db)
    
    from minibase.blueprints.user.models import User
    # import and register all blueprints.
    from minibase.blueprints.main.routes import main
    from minibase.blueprints.user.routes import user
    app.register_blueprint(main, url_prefix='/')
    app.register_blueprint(user, url_prefix='/user')
    
    return app

