from flask import Flask, request
# from flask_cors import CORS
from pymongo import MongoClient
from main.tools import JsonResp
from jose import jwt
import os

from main.user.routes import user_blueprint

def create_app():

  app = Flask(__name__)
  app.config.from_pyfile("config/config.cfg")
  # cors = CORS(app, resources={r"/*": { "origins": app.config["FRONTEND_DOMAIN"] }})

  # Misc Config
  # os.environ["TZ"] = app.config["TIMEZONE"]

  # Database Config
  if app.config["ENVIRONMENT"] == "development": #without id password
    mongo = MongoClient(app.config["MONGO_HOSTNAME"], app.config["MONGO_PORT"])
    app.db = mongo[app.config["MONGO_APP_DATABASE"]]
  else:
    mongo = MongoClient("localhost")
    mongo[app.config["MONGO_AUTH_DATABASE"]].authenticate(app.config["MONGO_AUTH_USERNAME"], app.config["MONGO_AUTH_PASSWORD"])
    app.db = mongo[app.config["MONGO_APP_DATABASE"]]

  app.register_blueprint(user_blueprint, url_prefix="/user")
  @app.route("/")
  def index():
    return JsonResp({ "status": "Online" }, 200)
  
  return app