from flask import Blueprint

from backend.utils.logging_utils import get_logger

logger = get_logger(__name__)


def register_routes(mode: str):
    """
    Register API routes conditionally based on the runtime mode ("agenthub" or "answers").

    Each route module imports its related services and ORM models.
    Importing a route therefore pulls in only the models that are actually used.

    Only imported models are registered in SQLAlchemy's metadata and have tables created at startup.
    This keeps the "answers" mode lightweight while preserving full functionality in "agenthub" mode.
    """
    logger.info(f"registering routes in {mode} mode.")

    api = Blueprint("api", __name__, url_prefix="/api")

    # Shared routes between ASW and AH
    from backend.routes.admin import admin_bp
    from backend.routes.app_config import config_bp
    from backend.routes.charts import chart_bp
    from backend.routes.conversations import convs_blueprint
    from backend.routes.downloads import downloads_bp
    from backend.routes.health import health_bp
    from backend.routes.prompts import prompts_bp
    from backend.routes.user import users_bp

    api.register_blueprint(users_bp)
    api.register_blueprint(convs_blueprint)
    api.register_blueprint(downloads_bp)
    api.register_blueprint(admin_bp)
    api.register_blueprint(config_bp)
    api.register_blueprint(chart_bp)
    api.register_blueprint(health_bp)
    api.register_blueprint(prompts_bp)

    # Routes specific to ASW
    if mode == "answers":
        pass
    else:  # Routes specific to AH
        from backend.routes.agenthub_agents import agenthub_agents_bp, agenthub_search_bp
        from backend.routes.dashboard import dashboard_bp
        from backend.routes.stories import stories_blueprint

        api.register_blueprint(stories_blueprint)
        api.register_blueprint(dashboard_bp)
        api.register_blueprint(agenthub_search_bp)
        api.register_blueprint(agenthub_agents_bp)

    return api
