from fastapi import APIRouter, Depends
from datetime import datetime

from app.api import Response, get_current_user
from app.models.base_model import get_db
from app.models.group_model import GroupModel, GroupData, GroupUsers, GroupResource
from app.models.user import PageParameter
from app.models.user_model import UserModel
from app.service.group import create_group, group_list, edit_group_data, delete_group_data, get_group_users, \
    save_user_to_group, save_group_resource

group_router = APIRouter()


@group_router.get("/list", response_model=Response)
async def user_group_list(current:int, pageSize:int, keyword="", current_user: UserModel = Depends(get_current_user),
                          db=Depends(get_db)):
    return Response(code=200, msg="", data=await group_list(db, pageSize, current, keyword))


@group_router.post("/add_group", response_model=Response)
async def add_group(group: GroupData, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):
    if not group.groupName:
        return Response(code=400, msg="The group_name cannot be empty!")
    db_group = db.query(GroupModel).filter(GroupModel.group_name == group.groupName).first()
    if db_group:
        return Response(code=400, msg="group already created")
    is_create = await create_group(db, group.groupName, group.description, current_user.id)
    if not is_create:
        return Response(code=200, msg="group create failure", data={})
    return Response(code=200, msg="group create successfully", data={"group_name": group.groupName})


@group_router.put("/edit_group", response_model=Response)
async def edit_group(group: GroupData, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):
    if not group.groupName:
        return Response(code=400, msg="The group_name cannot be empty!")
    db_group = db.query(GroupModel).filter(GroupModel.group_name == group.groupName).first()
    if db_group and db_group.id != group.id:
        return Response(code=400, msg="group_name already created")
    is_edit = await edit_group_data(db, group.id,
                                    {"group_name": group.groupName, "group_description": group.description,
                                     "updated_at": datetime.now()})
    if not is_edit:
        return Response(code=200, msg="group edit failure", data={})
    return Response(code=200, msg="group edit successfully", data={"group_name": group.groupName})


@group_router.put("/edit_group_status", response_model=Response)
async def edit_group_status(group: GroupData, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):
    if group.status not in ["0", "1"]:
        return Response(code=400, msg="The status cannot be {}!".format(group.status))
    db_group = db.query(GroupModel).filter(GroupModel.id == group.id).first()
    if not db_group:
        return Response(code=400, msg="group does not exist")
    is_edit = await edit_group_data(db, group.id,
                                    {"status": group.status,"updated_at": datetime.now()})
    if not is_edit:
        return Response(code=200, msg="group status edit failure", data={})
    return Response(code=200, msg="group status edit successfully", data={"group_name": group.groupName})


@group_router.delete("/delete_group/{groupId}", response_model=Response)
async def delete_group(groupId, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):
    db_group = db.query(GroupModel).filter(GroupModel.id == groupId).first()
    if not db_group:
        return Response(code=200, msg="group does not exist")
    if db_group.group_type ==2:
        return Response(code=400, msg="默认组无法删除！")
    is_edit = await delete_group_data(db, groupId)
    if not is_edit:
        return Response(code=200, msg="group delete failure", data={})
    return Response(code=200, msg="group delete successfully", data={})


@group_router.get("/group_users", response_model=Response)
async def group_users(groupId:int, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):
    db_group = db.query(GroupModel).filter(GroupModel.id == groupId).first()
    if not db_group:
        return Response(code=200, msg="group does not exist", data={})
    return Response(code=200, msg="success", data=await get_group_users(db, groupId))


@group_router.post("/save_group_user", response_model=Response)
async def save_group_user(group_user: GroupUsers, current_user: UserModel = Depends(get_current_user),
                          db=Depends(get_db)):
    db_group = db.query(GroupModel).filter(GroupModel.id == group_user.id).first()
    if not db_group:
        return Response(code=200, msg="group does not exist")
    is_success = await save_user_to_group(db, current_user.id, group_user.id, group_user.userList)
    if not is_success:
        return Response(code=500, msg="save user to group failure", data={})
    return Response(code=200, msg="success", data={})


@group_router.put("/group_resource", response_model=Response)
async def group_resource(group_resource: GroupResource, current_user: UserModel = Depends(get_current_user),
                          db=Depends(get_db)):
    db_group = db.query(GroupModel).filter(GroupModel.id == group_resource.id).first()
    if not db_group:
        return Response(code=200, msg="group does not exist")
    is_success = await save_group_resource(db, current_user.id, group_resource.id, group_resource.llms, group_resource.dialogs, group_resource.knowledges)
    if not is_success:
        return Response(code=500, msg="save user to group failure", data={})
    return Response(code=200, msg="success", data={})
