from flask import Blueprint, request, jsonify
from settings import SettingsManager

settings_bp = Blueprint('settings', __name__)


@settings_bp.route('/settings', methods=['GET'])
def get_all_settings():
    return jsonify({'status': 'success', 'settings': SettingsManager().get_all_settings()})

@settings_bp.route('/settings/<name>', methods=['GET'])
def get_setting(name):
    value = SettingsManager().get_settings(name)
    if value is not None:
        return jsonify({'status': 'success', 'name': name, 'value': value})
    else:
        return jsonify({'status': 'error', 'message': f'Setting "{name}" not found'}), 404

@settings_bp.route('/settings/update', methods=['POST'])
def set_all_settings():
    settings = request.json.get('settings')
    print (f"Received settings update: {settings}")
    if settings is None:
        return jsonify({'status': 'error', 'message': 'Settings are required'}), 400
    try:
        for name, value in settings.items():
            print(f"Updating setting '{name}' to '{value}'")
            SettingsManager().set_settings(name, value)
        return jsonify({'status': 'success', 'settings': settings})
    except ValueError as e:
        return jsonify({'status': 'error', 'message': str(e)}), 400
    