import cqi
from app import socketio
from app.decorators import socketio_login_required
from . import NAMESPACE as ns
from .utils import cqi_over_socketio


@socketio.on('cqi.corpora.get', namespace=ns)
@socketio_login_required
@cqi_over_socketio
def cqi_corpora_get(cqi_client: cqi.CQiClient, corpus_name: str):
    cqi_corpus = cqi_client.corpora.get(corpus_name)
    payload = {**cqi_corpus.attrs}
    return {'code': 200, 'msg': 'OK', 'payload': payload}


@socketio.on('cqi.corpora.list', namespace=ns)
@socketio_login_required
@cqi_over_socketio
def cqi_corpora_list(cqi_client: cqi.CQiClient):
    payload = [{**x.attrs} for x in cqi_client.corpora.list()]
    return {'code': 200, 'msg': 'OK', 'payload': payload}
