"""
Flask application setup.
"""

import logging
import os

from flask import Flask


def setup_app(app: Flask) -> None:
    is_local_dev = os.getenv("LOCAL_DEV", "").lower() == "true"

    if is_local_dev:
        from flask_cors import CORS

        from semantic_models.config import load_local_config

        # Enable CORS for local development
        CORS(
            app,
            resources={r"/api/*": {"origins": "*"}},
            methods=["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"],
            allow_headers=["Content-Type", "Authorization"],
        )
        app_config = load_local_config()
    else:
        from semantic_models.config import load_webapp_config

        # Load config from DSS webapp config
        app_config = load_webapp_config()

    # Register all API routes
    from semantic_models.routes import register_all_routes

    register_all_routes(app)

    # Configure logging
    from semantic_models.logging_utils import NoParsingFilter, configure_logging, install_request_logging_hooks

    configure_logging()
    install_request_logging_hooks(app)
    logging.getLogger("werkzeug").addFilter(NoParsingFilter())
    logging.info("Semantic Models application initialized successfully")
