"""
EN: API routes for the BBW University Research Platform backend.
    - Provides endpoints for resource loading and price calculation.
    - Integrates with Flask app and resource manager.
DE: API-Routen für das Backend der Forschungsplattform der bbw Hochschule.
    - Stellt Endpunkte für das Laden von Ressourcen und die Preiskalkulation bereit.
    - Integriert sich in die Flask-App und den Ressourcenmanager.
"""

from flask import Blueprint, jsonify, current_app
from app.api.calculate import calculate_price

api_blueprint = Blueprint('api', __name__)

def get_resources():
    """
    EN: Returns available printers, materials, and qualities for the UI.
    DE: Gibt verfügbare Drucker, Materialien und Qualitäten für die UI zurück.
    """
    try:
        ressourcen = current_app.ressourcen

        printers_list = ressourcen.get_all_drucker_names()
        qualities_list = ressourcen.get_all_qualitaet_names()

        materials_list = ressourcen.get_all_materials_for_ui()

        return jsonify({
            "printers": printers_list,
            "materials": materials_list,
            "qualities": qualities_list
        })
    except Exception as e:
        current_app.logger.error(f"Error while fetching resources: {e}")
        return jsonify({"error": "Could not load resources from server."}), 500

api_blueprint.add_url_rule('/resources', 'get_resources', get_resources, methods=['GET'])
api_blueprint.add_url_rule('/calculate', 'calculate_price', calculate_price, methods=['POST'])