from datetime import datetime
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
from module_admin.entity.vo.common_vo import CrudResponseModel
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.login_service import LoginService
from module_admin.service.message_service import MessageService
from module_admin.entity.vo.message_vo import DeleteMessageModel, MessageModel, MessagePageQueryModel, EditMessageModel
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


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


# 查看告警类型分布
@messageController.get(
    '/alarm_type_distribution'
)
async def get_alarm_type_distribution(
    request: Request,
    query_db: AsyncSession = Depends(get_db),
):
    result = await MessageService.get_alarm_type_distribution_services(query_db)
    return ResponseUtil.success(data=result)



# 一键处理未处理的告警消息
@messageController.get(
    '/handle_all'
)
async def handle_all_message(
    query_db: AsyncSession = Depends(get_db),
):
    result =  await MessageService.handle_all_message_services(query_db)
    return ResponseUtil.success(msg= result.message)


# 查询告警数量信息
@messageController.get(
    '/count_message',
)
async def count_message(request: Request, query_db: AsyncSession = Depends(get_db)):
    """"""
    count_message_result = await MessageService.count_message(query_db)
    return ResponseUtil.success(data = count_message_result)



# # 获取所有消息列表
# @messageController.get(
#     '/list', response_model=PageResponseModel
#     # , dependencies=[Depends(CheckUserInterfaceAuth('system:message:list'))]
# )
# async def get_system_message_list(
#     request: Request,
#     message_page_query: MessagePageQueryModel = Depends(MessagePageQueryModel.as_query),
#     query_db: AsyncSession = Depends(get_db),
# ):
#     # 获取分页数据
#     message_page_query_result = await MessageService.get_message_list_services(query_db, message_page_query, is_page=True)
#     logger.info('获取成功')

#     return ResponseUtil.success(model_content=message_page_query_result)

# 获取告警消息列表
@messageController.get('/alert_message')
async def get_alert_message(
    request: Request,
    pageNum: int = 1,
    pageSize: int = 10,
    query_db: AsyncSession = Depends(get_db),
):
    alert_message = await MessageService.get_alert_message_services(query_db, pageNum, pageSize)
    return ResponseUtil.success(model_content=alert_message)


# 该接口只做测试用, 并不对外开放
# @messageController.post('/add_message'
#     # , dependencies=[Depends(CheckUserInterfaceAuth('system:message:add'))]
#                         )
# @ValidateFields(validate_model='add_message')
# @Log(title='系统消息', business_type=BusinessType.INSERT)
# async def add_system_message(
#     request: Request,
#     add_message: MessageModel,
#     query_db: AsyncSession = Depends(get_db),
#     current_user: CurrentUserModel = Depends(LoginService.get_current_user),
# ):
#     add_message.create_time = datetime.now()
#     add_message.create_by = current_user.user.user_name
#     add_message.update_time = datetime.now()
#     add_message.update_by = current_user.user.user_name
#     add_message.status = '0'
#     add_message_result = await MessageService.add_message_services(query_db, add_message)
#     logger.info(add_message_result.message)

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


# @messageController.put('/edit_message'
#     # , dependencies=[Depends(CheckUserInterfaceAuth('system:message:edit'))]
#                        )
# @ValidateFields(validate_model='edit_message')
# @Log(title='系统消息', business_type=BusinessType.UPDATE)
# async def edit_system_message(
#     request: Request,
#     edit_message: EditMessageModel,
#     query_db: AsyncSession = Depends(get_db),
#     current_user: CurrentUserModel = Depends(LoginService.get_current_user),
# ):
#     edit_message.update_by = current_user.user.user_name
#     edit_message.update_time = datetime.now()
#     edit_message_result = await MessageService.edit_message_services(query_db, edit_message)
#     logger.info(edit_message_result.message)

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


# @messageController.delete('/{message_ids}'
#     , dependencies=[Depends(CheckUserInterfaceAuth('system:message:remove'))]
#                           )
# @Log(title='系统消息', business_type=BusinessType.DELETE)
# async def delete_system_message(request: Request, message_ids: str, query_db: AsyncSession = Depends(get_db)):
#     delete_message = DeleteMessageModel(messageIds=message_ids)
#     delete_message_result = await MessageService.delete_message_services(query_db, delete_message)
#     logger.info(delete_message_result.message)

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


# @messageController.get(
#     '/{message_id}', response_model=MessageModel
#     # , dependencies=[Depends(CheckUserInterfaceAuth('system:message:query'))]
# )
# async def query_detail_system_message(request: Request, message_id: int, query_db: AsyncSession = Depends(get_db)):
#     message_detail_result = await MessageService.message_detail_services(query_db, message_id)
#     logger.info(f'获取message_id为{message_id}的信息成功')

#     return ResponseUtil.success(data=message_detail_result)


# @messageController.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('system:message:export'))])
# @Log(title='系统消息', business_type=BusinessType.EXPORT)
# async def export_system_message_list(
#     request: Request,
#     message_page_query: MessagePageQueryModel = Form(),
#     query_db: AsyncSession = Depends(get_db),
# ):
#     # 获取全量数据
#     message_query_result = await MessageService.get_message_list_services(query_db, message_page_query, is_page=False)
#     message_export_result = await MessageService.export_message_list_services(message_query_result)
#     logger.info('导出成功')
#
#     return ResponseUtil.streaming(data=bytes2file_response(message_export_result))
