from flask import Blueprint, request, jsonify
from ..rag.store import vector_search, ingest_documents

rag_bp = Blueprint('rag', __name__)

@rag_bp.route('/ingest', methods=['POST'])
def ingest():
    data = request.json
    text_chunks = data.get('chunks', [])
    if not text_chunks:
        return jsonify({"error": "No chunks provided"}), 400
    
    count = ingest_documents(text_chunks)
    return jsonify({"message": f"Ingested {count} documents"}), 201

@rag_bp.route('/search', methods=['POST'])
def search():
    data = request.json
    query = data.get('query')
    if not query:
        return jsonify({"error": "No query provided"}), 400
    
    results = vector_search(query)
    return jsonify({"results": results}), 200
