import os
from flask import Flask

import os
import requests
from app import config
from app.routes import default
from app.routes import debug
from app.routes import auth
from app.routes import admin
from app.routes import profile
from app.routes import server
from app.pages import create_pages
from sqlmodel import SQLModel
from sqlmodel import create_engine

from app.models.user import Log, User

APP_SETTINGS = os.getenv("APP_SETTINGS", "Testing")


def drop_database(config):
    import sqlalchemy
    from sqlalchemy import create_engine
    from sqlalchemy import MetaData
    from sqlalchemy import inspect
    from sqlalchemy.orm import sessionmaker
    from sqlalchemy.ext.declarative import declarative_base

    engine = create_engine(config["DATABASE_URI"])
    meta = MetaData()
    meta.reflect(bind=engine)
    meta.drop_all(engine, checkfirst=False)


def create_app():
    app = Flask(__name__, template_folder="app/templates/", static_folder="app/static/")
    app.count_requests = 0

    app.config.from_object(f"app.config.{APP_SETTINGS}")
    app.secret_key = os.urandom(256)
    app.url_map.strict_slashes = False

    app.register_blueprint(default.bp)
    app.register_blueprint(debug.bp, url_prefix="/debug")
    app.register_blueprint(auth.bp, url_prefix="/auth")
    app.register_blueprint(admin.bp, url_prefix="/admin")
    app.register_blueprint(profile.bp, url_prefix="/user")
    app.register_blueprint(server.bp, url_prefix="/server")
    app.register_error_handler(Exception, default.handle_exception)

    return app


app = create_app()
app.pages = create_pages()
with app.app_context():
    from app.database import engine

    app.engine = engine

if os.getenv("DROP_DATABASE", False):
    drop_database(app.config)

SQLModel.metadata.create_all(engine)

if __name__ == "__main__":
    app.run(host=app.config.get("HOST"), port=app.config.get("PORT"))

os.getenv flask.Flask os.urandom sqlalchemy.create_engine sqlalchemy.MetaData sqlmodel.SQLModel.metadata.create_all app.pages.create_pages
[((417, 453), 'os.getenv', 'os.getenv', (['"""APP_SETTINGS"""', '"""Testing"""'], {}), "('APP_SETTINGS', 'Testing')\n", (426, 453), False, 'import os\n'), ((1587, 1601), 'app.pages.create_pages', 'create_pages', ([], {}), '()\n', (1599, 1601), False, 'from app.pages import create_pages\n'), ((1691, 1724), 'os.getenv', 'os.getenv', (['"""DROP_DATABASE"""', '(False)'], {}), "('DROP_DATABASE', False)\n", (1700, 1724), False, 'import os\n'), ((1757, 1793), 'sqlmodel.SQLModel.metadata.create_all', 'SQLModel.metadata.create_all', (['engine'], {}), '(engine)\n', (1785, 1793), False, 'from sqlmodel import SQLModel\n'), ((735, 772), 'sqlalchemy.create_engine', 'create_engine', (["config['DATABASE_URI']"], {}), "(config['DATABASE_URI'])\n", (748, 772), False, 'from sqlalchemy import create_engine\n'), ((784, 794), 'sqlalchemy.MetaData', 'MetaData', ([], {}), '()\n', (792, 794), False, 'from sqlalchemy import MetaData\n'), ((899, 977), 'flask.Flask', 'Flask', (['__name__'], {'template_folder': '"""app/templates/"""', 'static_folder': '"""app/static/"""'}), "(__name__, template_folder='app/templates/', static_folder='app/static/')\n", (904, 977), False, 'from flask import Flask\n'), ((1084, 1099), 'os.urandom', 'os.urandom', (['(256)'], {}), '(256)\n', (1094, 1099), False, 'import os\n')]