from flask_sqlalchemy import SQLAlchemy
from flask_user import UserManager, SQLAlchemyAdapter
from flask import Flask
from flask_mail import Mail
from slapos.certificate_authority.web.settings import BaseConfig
import os

# CA_INSTANCE_PATH is the base directory of application, send to environ
app = Flask(__name__,
            instance_path=config_name = os.getenv('CA_INSTANCE_PATH', os.getcwd()),
            instance_relative_config=True)

# Use default value so SQLALCHEMY will not warn because there is not db_uri
app.config['SQLALCHEMY_DATABASE_URI'] = BaseConfig.SQLALCHEMY_DATABASE_URI
db = SQLAlchemy(app)

def init_app():
  mail = Mail(app)
  #db.init_app(app)
  db.create_all()

  # Setup Flask-User
  from slapos.certificate_authority.web.models import User
  db_adapter = SQLAlchemyAdapter(db, User)        # Register the User model
  user_manager = UserManager(db_adapter, app)     # Initialize Flask-User