from fastapi import APIRouter, Depends, Form, Request
from pydantic_validation_decorator import ValidateFields
from sqlalchemy.ext.asyncio import AsyncSession
from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth, CheckRoleInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.login_service import LoginService
from module_admin.service.robot_action_service import Robot_actionService
from module_admin.entity.vo.robot_action_vo import DeleteRobot_actionModel, Robot_actionModel, Robot_actionPageQueryModel
from utils.common_util import bytes2file_response
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil

from datetime import datetime


robot_actionController = APIRouter(prefix='/system/robot_action', dependencies=[Depends(LoginService.get_current_user)])


@robot_actionController.get(
    '/list', response_model=PageResponseModel
    # , dependencies=[Depends(CheckUserInterfaceAuth('system:robot_action:list'))]
)
async def get_system_robot_action_list(
    request: Request,
    robot_action_page_query: Robot_actionPageQueryModel = Depends(Robot_actionPageQueryModel.as_query),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取分页数据
    robot_action_page_query_result = await Robot_actionService.get_robot_action_list_services(query_db, robot_action_page_query, is_page=False)
    logger.info('获取成功')

    return ResponseUtil.success(data=robot_action_page_query_result)


# @robot_actionController.post('', dependencies=[Depends(CheckUserInterfaceAuth('system:robot_action:add'))])
# @ValidateFields(validate_model='add_robot_action')
# @Log(title='机器人动作', business_type=BusinessType.INSERT)
# async def add_system_robot_action(
#     request: Request,
#     add_robot_action: Robot_actionModel,
#     query_db: AsyncSession = Depends(get_db),
#     current_user: CurrentUserModel = Depends(LoginService.get_current_user),
# ):
#     add_robot_action.update_time = datetime.now()
#     add_robot_action_result = await Robot_actionService.add_robot_action_services(query_db, add_robot_action)
#     logger.info(add_robot_action_result.message)

#     return ResponseUtil.success(msg=add_robot_action_result.message)


@robot_actionController.put(''
                            # , dependencies=[Depends(CheckUserInterfaceAuth('system:robot_action:edit'))]
                            )
@ValidateFields(validate_model='edit_robot_action')
@Log(title='机器人动作', business_type=BusinessType.UPDATE)
async def edit_system_robot_action(
    request: Request,
    edit_robot_action: Robot_actionModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    edit_robot_action.update_by = current_user.user.user_name
    edit_robot_action.update_time = datetime.now()
    edit_robot_action_result = await Robot_actionService.edit_robot_action_services(query_db, edit_robot_action)
    logger.info(edit_robot_action_result.message)

    return ResponseUtil.success(msg=edit_robot_action_result.message)


# @robot_actionController.delete('/{ids}', dependencies=[Depends(CheckUserInterfaceAuth('system:robot_action:remove'))])
# @Log(title='机器人动作', business_type=BusinessType.DELETE)
# async def delete_system_robot_action(request: Request, ids: str, query_db: AsyncSession = Depends(get_db)):
#     delete_robot_action = DeleteRobot_actionModel(ids=ids)
#     delete_robot_action_result = await Robot_actionService.delete_robot_action_services(query_db, delete_robot_action)
#     logger.info(delete_robot_action_result.message)

#     return ResponseUtil.success(msg=delete_robot_action_result.message)


# @robot_actionController.get(
#     '/{id}', response_model=Robot_actionModel, dependencies=[Depends(CheckUserInterfaceAuth('system:robot_action:query'))]
# )
# async def query_detail_system_robot_action(request: Request, id: int, query_db: AsyncSession = Depends(get_db)):
#     robot_action_detail_result = await Robot_actionService.robot_action_detail_services(query_db, id)
#     logger.info(f'获取id为{id}的信息成功')

#     return ResponseUtil.success(data=robot_action_detail_result)


# @robot_actionController.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('system:robot_action:export'))])
# @Log(title='机器人动作', business_type=BusinessType.EXPORT)
# async def export_system_robot_action_list(
#     request: Request,
#     robot_action_page_query: Robot_actionPageQueryModel = Form(),
#     query_db: AsyncSession = Depends(get_db),
# ):
#     # 获取全量数据
#     robot_action_query_result = await Robot_actionService.get_robot_action_list_services(query_db, robot_action_page_query, is_page=False)
#     robot_action_export_result = await Robot_actionService.export_robot_action_list_services(robot_action_query_result)
#     logger.info('导出成功')

#     return ResponseUtil.streaming(data=bytes2file_response(robot_action_export_result))
