import connexion
import prance

from pathlib import Path
from .config import get_config
from .db import db, migrate
from flask_cors import CORS


def get_bundled_specs(main_file):
    parser = prance.ResolvingParser(str(main_file.absolute()), lazy=True, strict=True)
    parser.parse()
    return parser.specification


def create_app():
    connexionApp = connexion.FlaskApp(__name__)
    app = connexionApp.app
    connexionApp.add_api(get_bundled_specs(Path('api/openapi/api.yaml')))
    CORS(app)
    app.config.from_object(get_config())
    db.init_app(app)
    migrate.init_app(app, db, directory='api/db/migrations')
    return app
