"""
Flask Application Factory

This module provides a factory function, create_app, which is responsible for creating and configuring the Flask application.
The application is configured based on core from the config.py file, and extensions like Flask-RESTx, Flask-JWT-Extended,
Flask-MongoEngine, and Flask-CORS are initialized.

Usage:
    from app import create_app
    app = create_app()
"""

from flask import Flask # Import the Flask class from the Flask package

from app.resources.v1 import initialize_api # Import the initialize_api function from the app.resources module
from app.core.config import get_config # Import the get_config function from the app.core.config module
from app.core.extensions import db, cors # Import the db, and cors objects from the app.core.extensions module 

def create_app():
    """
    Create and configure the Flask application.

    This function initializes a Flask application instance, configures it based on settings from config.py, and initializes
    various extensions such as Flask-RESTx, Flask-JWT-Extended, Flask-MongoEngine, and Flask-CORS. It also includes the authentication
    namespace from the 'app.resources.auth' module.

    Returns:
        Flask: The configured Flask app.

    Notes:
        The function should be called to create the Flask app instance when starting the application.

    Example:
        from app import create_app
        app = create_app()
    """
    # Create a Flask app instance
    app = Flask(__name__)  
    # Load the configuration settings into the Flask app from config.py
    app.config.from_object(get_config())  
    # Initialize Flask-CORS
    cors.init_app(app) 
    # Initialize Database
    db.init_app(app)   
    # Initialize 
    with app.app_context():
        db.create_all()
    # Initialize the APP API (Initialize the Flask-RESTx API)
    initialize_api(app) 
    # Return the configured Flask app instance
    return app 