import os
import json
from flask import Flask, redirect, url_for, make_response
from flask_cors import CORS
from flask_sqlalchemy import SQLAlchemy
from data_model.db_init import add_db_cli_commands

db = SQLAlchemy()


def create_app(test_config=None):
    # create and configure the api
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_mapping(
        DBML_SCHEMA=os.path.join(os.path.dirname(app.root_path), 'data_model', 'dgi_schema.dbml'),
        DATA_MODEL=os.path.join('model.py'),
        SQLALCHEMY_TRACK_MODIFICATIONS=False
    )

    if test_config is None:
        # load the instance config, if it exists, when not testing
        app.config.from_pyfile('config.py', silent=True)
    else:
        # load the test config if passed in
        app.config.from_mapping(test_config)

    # ensure the instance folder exists
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass

    from .helpers import usd
    from .auth import auth_bp
    from api.v1.accounts import accounts_v1_bp
    from api.v1.securities import securities_v1_bp

    CORS(app)
    db.init_app(app)
    add_db_cli_commands(app, db)
    app.register_blueprint(auth_bp)
    app.register_blueprint(accounts_v1_bp)
    app.register_blueprint(securities_v1_bp)
    app.jinja_env.filters["usd"] = usd

    # Ensure responses aren't cached
    @app.after_request
    def after_request(response):
        response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
        response.headers["Expires"] = 0
        response.headers["Pragma"] = "no-cache"
        return response

    @app.route('/')
    def forward():
        # forward to recent api
        return redirect(url_for('securities_api_v1.quote'))

    @app.route('/debug')
    def debug():
        # a simple page that shows app configurations
        print(app.config)
        return json.dumps(app.config, default=str)

    @app.route('/nightly_update')
    def nightly_update():
        from api.worker import Worker
        worker = Worker(db)
        worker.nightly_update()
        return make_response("Nightly update ran successfully!", 200)

    return app

import os
import json
from flask import Flask, redirect, url_for, make_response
from flask_cors import CORS
from flask_sqlalchemy import SQLAlchemy
from data_model.db_init import add_db_cli_commands

db = SQLAlchemy()


def create_app(test_config=None):
    # create and configure the api
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_mapping(
        DBML_SCHEMA=os.path.join(os.path.dirname(app.root_path), 'data_model', 'dgi_schema.dbml'),
        DATA_MODEL=os.path.join('model.py'),
        SQLALCHEMY_TRACK_MODIFICATIONS=False
    )

    if test_config is None:
        # load the instance config, if it exists, when not testing
        app.config.from_pyfile('config.py', silent=True)
    else:
        # load the test config if passed in
        app.config.from_mapping(test_config)

    # ensure the instance folder exists
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass

    from .helpers import usd
    from .auth import auth_bp
    from api.v1.accounts import accounts_v1_bp
    from api.v1.securities import securities_v1_bp

    CORS(app)
    db.init_app(app)
    add_db_cli_commands(app, db)
    app.register_blueprint(auth_bp)
    app.register_blueprint(accounts_v1_bp)
    app.register_blueprint(securities_v1_bp)
    app.jinja_env.filters["usd"] = usd

    # Ensure responses aren't cached
    @app.after_request
    def after_request(response):
        response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
        response.headers["Expires"] = 0
        response.headers["Pragma"] = "no-cache"
        return response

    @app.route('/')
    def forward():
        # forward to recent api
        return redirect(url_for('securities_api_v1.quote'))

    @app.route('/debug')
    def debug():
        # a simple page that shows app configurations
        print(app.config)
        return json.dumps(app.config, default=str)

    @app.route('/nightly_update')
    def nightly_update():
        from api.worker import Worker
        worker = Worker(db)
        worker.nightly_update()
        return make_response("Nightly update ran successfully!", 200)

    return app

# create and configure the api # load the instance config, if it exists, when not testing # load the test config if passed in # ensure the instance folder exists # Ensure responses aren't cached # forward to recent api # a simple page that shows app configurations