from datetime import datetime

from sqlalchemy import or_

from app.models import KnowledgeModel, group_knowledge_table, KnowledgeUserModel
from app.models.user_model import UserModel
from Log import logger
from app.task.fetch_agent import get_one_from_ragflow_knowledge


async def get_knowledge_list(db, user_id, keyword, page_size, page_index, status, knowledge, location):
    user = db.query(UserModel).filter(UserModel.id == user_id).first()
    if user is None:
        return {"rows": []}
    query = db.query(KnowledgeModel).filter(KnowledgeModel.knowledge_type==knowledge)
    if user.permission != "admin":
        klg_list = [j.id for i in user.groups for j in i.knowledges]
        for i in db.query(KnowledgeUserModel).filter(KnowledgeUserModel.user_id == user_id, KnowledgeUserModel.status == 1).all():
            if i.kb_id not in klg_list:
                klg_list.append(i.kb_id)
        query = query.filter(or_(KnowledgeModel.id.in_(klg_list), KnowledgeModel.tenant_id == str(user_id)))

        if location:
            query = query.filter(or_(KnowledgeModel.permission == "team", KnowledgeModel.tenant_id == str(user_id)))

    if keyword:
        query = query.filter(KnowledgeModel.name.like('%{}%'.format(keyword)))
    total = query.count()
    if page_size:
        query = query.order_by(KnowledgeModel.update_date.desc()).limit(page_size).offset((page_index - 1) * page_size)
    return {"total":  total, "rows": [kld.to_json(str(user_id) if user.permission != "admin" else "admin") for kld in query.all()]}


async def create_knowledge_service(db, klg_id, name, description, icon, klg_type, user_id):
    try:
        dialog_model = KnowledgeModel(id=klg_id,name=name, description=description,icon=icon, knowledge_type=klg_type, tenant_id=user_id,update_date=datetime.now(),create_date=datetime.now())
        db.add(dialog_model)
        kb_user = KnowledgeUserModel(kb_id=klg_id, user_id=user_id, status=1, update_date=datetime.now(),
                                     create_date=datetime.now())
        db.add(kb_user)
        db.commit()
        db.refresh(dialog_model)
    except Exception as e:
        logger.error(e)
        db.rollback()
        return False
    return True


async def sync_knowledge_service(db, klg_id):
    knowledge = db.query(KnowledgeModel).filter(KnowledgeModel.id == klg_id).first()
    if knowledge:
        try:
            app_knowledge = get_one_from_ragflow_knowledge(klg_id)
            if app_knowledge:
                knowledge.name = app_knowledge["name"]
                knowledge.description = app_knowledge["description"]
                knowledge.permission = app_knowledge["permission"]
                knowledge.documents = app_knowledge["doc_num"]
                knowledge.status = app_knowledge["status"]
                knowledge.embd_id = app_knowledge["embd_id"]
                knowledge.update_date = datetime.now()
                db.add(knowledge)
                db.commit()
                db.refresh(knowledge)
        except Exception as e:
            logger.error(e)
            db.rollback()
            return False
        return True


async def delete_knowledge_service(db, klg_id):

    try:
        db.query(KnowledgeModel).filter(KnowledgeModel.id==klg_id).delete()
        db.commit()
    except Exception as e:
        logger.error(e)
        db.rollback()
        return False
    return True



async def update_knowledge_icon_service(db, klg_id, icon):
    try:
        db.query(KnowledgeModel).filter_by(id=klg_id).update({"icon":icon, "update_date": datetime.now()})
        db.commit()
    except Exception as e:
        logger.error(e)
        db.rollback()
        return False
    return True



async def get_knowledge_users_service(db, kb_id, user_id, page, page_size, member):
    kb_user_list = []
    user_ids = []
    kb_info = db.query(KnowledgeModel).filter(KnowledgeModel.id == kb_id).first()
    query = db.query(KnowledgeUserModel).filter(KnowledgeUserModel.kb_id == kb_id)
    if member:
        query = query.filter(KnowledgeUserModel.user_id != user_id)
    total = query.count()

    for kld in query.limit(page_size).offset((page - 1) * page_size).all():
        user_ids.append(kld.user_id)
        print(kb_info.tenant_id)
        kb_user_list.append(kld.to_json(kb_info.tenant_id))
    user = {i.id: i.username for i in db.query(UserModel).filter(UserModel.id.in_(user_ids)).all()}
    for kld in kb_user_list:
        kld["username"] = user.get(kld["user_id"], "")
    return {"total": total, "rows": kb_user_list}


async def set_knowledge_owner_service(db, kb_id, user_id, u_id):
    try:
        db.query(KnowledgeModel).filter_by(id=kb_id).update({"tenant_id": str(user_id), "update_date": datetime.now()})
        db.commit()
    except Exception as e:
        logger.error(e)
        db.rollback()
        return False
    return True


async def add_knowledge_user_service(db, kb_id, user_id):
    try:

        kb_user = KnowledgeUserModel(kb_id=kb_id, user_id=user_id, status=1, update_date=datetime.now(),
                                     create_date=datetime.now())
        db.add(kb_user)
        db.commit()
    except Exception as e:
        logger.error(e)
        db.rollback()
        return False
    return True