
from flask import Blueprint, request, jsonify
from metadata_manager import MetaDataManager

metadata_bp = Blueprint('metadata', __name__)

@metadata_bp.route('/meta', methods=['GET'])
def get_allmetadata():
    meta_manager = MetaDataManager()
    collections = meta_manager.collections
    return jsonify({'status': 'success', 'collections': collections})

@metadata_bp.route('/meta/collections', methods=['GET'])
def get_collections():
    meta_manager = MetaDataManager()
    collections = meta_manager.get_collections()
    return jsonify({'status': 'success', 'collections': collections})

@metadata_bp.route('/meta/collections/add', methods=['POST'])
def add_collection():
    meta_manager = MetaDataManager()
    collection_name = request.json.get('name')
    try:
        meta_manager.create_collection(collection_name)
        collections = meta_manager.collections
        return jsonify({'status': 'success', 'collections': collections})
    except ValueError as e:
        return jsonify({'status': 'error', 'message': str(e)}), 400

@metadata_bp.route('/meta/collection/clips/<name>', methods=['GET'])
def get_clips_in_collection(name):
    meta_manager = MetaDataManager()
    collection_name = name
    try:
        clips = meta_manager.get_clips_in_collection(collection_name)
        return jsonify({'status': 'success', 'clips': clips})
    except ValueError as e:
        return jsonify({'status': 'error', 'message': str(e)}), 400
    
@metadata_bp.route('/meta/collection/clips/reorder', methods=['POST'])
def reorder_clips_in_collection():
    meta_manager = MetaDataManager()
    collection_name = request.json.get('name')
    new_order = request.json.get('clips')
    try:
        meta_manager.reorder_clips_in_collection(collection_name, new_order)
        collections = meta_manager.collections
        return jsonify({'status': 'success', 'collections': collections})
    except ValueError as e:
        return jsonify({'status': 'error', 'message': str(e)}), 400

@metadata_bp.route('/meta/collection/clips/add', methods=['POST'])
def add_clip_to_collection():
    meta_manager = MetaDataManager()
    collection_name = request.json.get('name')
    clip_metadata = request.json.get('clip')
    try:
        meta_manager.add_clip_to_collection(collection_name, clip_metadata)
        collections = meta_manager.collections
        return jsonify({'status': 'success', 'collections': collections})
    except ValueError as e:
        return jsonify({'status': 'error', 'message': str(e)}), 400
    
@metadata_bp.route('/meta/collection/clips/remove', methods=['POST'])
def remove_clip_from_collection():
    meta_manager = MetaDataManager()
    collection_name = request.json.get('name')
    clip_metadata = request.json.get('clip')
    try:
        meta_manager.remove_clip_from_collection(collection_name, clip_metadata)
        clips = meta_manager.get_clips_in_collection(collection_name)
        return jsonify({'status': 'success', 'clips': clips})
    except ValueError as e:
        return jsonify({'status': 'error', 'message': str(e)}), 400
    
@metadata_bp.route('/meta/collection/clips/move', methods=['POST'])
def move_clip_to_collection():
    meta_manager = MetaDataManager()
    sourceCollection = request.json.get('sourceCollection')
    targetCollection = request.json.get('targetCollection')
    clip_metadata = request.json.get('clip')
    try:
        meta_manager.move_clip_to_collection(sourceCollection, targetCollection, clip_metadata)
        collections = meta_manager.collections
        return jsonify({'status': 'success', 'collections': collections})
    except ValueError as e:
        return jsonify({'status': 'error', 'message': str(e)}), 400
    
@metadata_bp.route('/meta/collection/clips/edit', methods=['POST'])
def edit_clip_in_collection():
    meta_manager = MetaDataManager()
    collection_name = request.json.get('name')
    clip_metadata = request.json.get('clip')
    # print(f"Received request to edit clip in collection '{collection_name}': {clip_metadata}")
    try:
        meta_manager.edit_clip_in_collection(collection_name, clip_metadata)
        collections = meta_manager.collections
        return jsonify({'status': 'success', 'collections': collections})
    except ValueError as e:
        return jsonify({'status': 'error', 'message': str(e)}), 400


@metadata_bp.route('/ws/test', methods=['POST'])
def test_websocket():
    MetaDataManager().socket.emit('test_event', {'data': 'Test message from metadata route'})
    return jsonify({'status': 'success'})