from flask import Flask
from config import Config
from sqlalchemy import MetaData
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager
from flask_moment import Moment
from flask_misaka import Misaka
from flask_bootstrap import Bootstrap
import os
import logging
from logging.handlers import RotatingFileHandler
from elasticsearch import Elasticsearch

convention = {
    "ix": 'ix_%(column_0_label)s',
    "uq": "uq_%(table_name)s_%(column_0_name)s",
    "ck": "ck_%(table_name)s_%(constraint_name)s",
    "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
    "pk": "pk_%(table_name)s"
}

metadata = MetaData(naming_convention=convention)

db = SQLAlchemy(metadata=metadata)
migrate = Migrate()
login = LoginManager()
login.login_view = "auth.login"
moment = Moment()
md = Misaka()
bootstrap = Bootstrap()

def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(Config)

    db.init_app(app)
    with app.app_context():
        if db.engine.url.drivername == 'sqlite':
            migrate.init_app(app, db, render_as_batch=True)
        else:
            migrate.init_app(app, db)
    # migrate.init_app(app, db)
    login.init_app(app)
    moment.init_app(app)
    md.init_app(app)
    bootstrap.init_app(app)

    from app.errors import bp as errors_bp
    app.register_blueprint(errors_bp)

    from app.auth import bp as auth_bp
    app.register_blueprint(auth_bp, url_prefix='/auth')

    from app.main import bp as main_bp
    app.register_blueprint(main_bp)

    from app.cli import bp as cli_bp
    app.register_blueprint(cli_bp)

    app.elasticsearch = Elasticsearch([app.config['ELASTICSEARCH_URL']]) \
        if app.config['ELASTICSEARCH_URL'] else None

    from app import models

    if not app.debug and not app.testing:
        if not os.path.exists("logs"):
            os.mkdir("logs")
        file_handler = RotatingFileHandler(
            "logs/moviedb.log", maxBytes=10240, backupCount=10
        )
        file_handler.setFormatter(
            logging.Formatter(
                "%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]"
            )
        )
        file_handler.setLevel(logging.INFO)
        app.logger.addHandler(file_handler)

        app.logger.setLevel(logging.INFO)
        app.logger.info("Moviedb startup")
    
    return app

[(23, 'sqlalchemy.MetaData', 'MetaData', False, 'from sqlalchemy import MetaData\n'), (25, 'flask_sqlalchemy.SQLAlchemy', 'SQLAlchemy', False, 'from flask_sqlalchemy import SQLAlchemy\n'), (26, 'flask_migrate.Migrate', 'Migrate', False, 'from flask_migrate import Migrate\n'), (27, 'flask_login.LoginManager', 'LoginManager', False, 'from flask_login import LoginManager\n'), (29, 'flask_moment.Moment', 'Moment', False, 'from flask_moment import Moment\n'), (30, 'flask_misaka.Misaka', 'Misaka', False, 'from flask_misaka import Misaka\n'), (31, 'flask_bootstrap.Bootstrap', 'Bootstrap', False, 'from flask_bootstrap import Bootstrap\n'), (34, 'flask.Flask', 'Flask', False, 'from flask import Flask\n'), (61, 'elasticsearch.Elasticsearch', 'Elasticsearch', False, 'from elasticsearch import Elasticsearch\n'), (69, 'logging.handlers.RotatingFileHandler', 'RotatingFileHandler', False, 'from logging.handlers import RotatingFileHandler\n'), (67, 'os.path.exists', 'os.path.exists', False, 'import os\n'), (68, 'os.mkdir', 'os.mkdir', False, 'import os\n'), (73, 'logging.Formatter', 'logging.Formatter', False, 'import logging\n')]