from datetime import datetime, date
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.user_vo import CurrentUserModel
from module_admin.service.login_service import LoginService
from module_admin.service.sys_statistics_service import Sys_statisticsService
from module_admin.entity.vo.sys_statistics_vo import DeleteSys_statisticsModel, Sys_statisticsModel, Sys_statisticsPageQueryModel
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


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


# 获取统计数据
@sys_statisticsController.get(
    '/get_data'
)
async def get_statistics_data(
    request: Request,
    query_db: AsyncSession = Depends(get_db),
):
    data = await Sys_statisticsService.get_statistics_data_services(query_db)
    return ResponseUtil.success(data=data)

# 获取访问趋势
@sys_statisticsController.get(
    '/get_visitor_count/{day}'
)
async def get_visitor_count(
    request: Request,
    day: int,
    query_db: AsyncSession = Depends(get_db),
):
    data = await Sys_statisticsService.get_visitor_count_services(query_db, day)
    return ResponseUtil.success(data=data)

# 获取门禁识别成功率
@sys_statisticsController.get(
    '/access_control_success_rate/{day}'
)
async def get_access_control_success_rate(
    request: Request,
    day: int,
    query_db: AsyncSession = Depends(get_db),
):
    data = await Sys_statisticsService.get_access_control_success_rate_services(query_db, day)
    return ResponseUtil.success(data=data)

# 大模型调用次数加1
@sys_statisticsController.get(
    '/add_model_call_count'
)
async def add_model_call_count(
    request: Request,
    query_db: AsyncSession = Depends(get_db),
    user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    data = await Sys_statisticsService.add_model_call_count_services(query_db, user.user.user_name)
    if data.is_success:
        logger.info('大模型调用次数加1成功')
        return ResponseUtil.success(msg=data.message)
    else:
        logger.error('大模型调用次数加1失败')
        return ResponseUtil.error(msg=data.message)
    
# 门禁通行量次数加1
@sys_statisticsController.get(
    '/add_door_pass_count'
)
async def add_door_pass_count(
    request: Request,
    query_db: AsyncSession = Depends(get_db),
    user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    data = await Sys_statisticsService.add_door_pass_count_services(query_db, user.user.user_name)
    if data.is_success:
        logger.info('门禁通行量次数加1成功')
        return ResponseUtil.success(msg=data.message)
    else:
        logger.error('门禁通行量次数加1失败')
        return ResponseUtil.error(msg=data.message)
# 访客引导次数加1
@sys_statisticsController.get(
    '/add_visitor_guide_count'
)
async def add_visitor_guide_count(
    request: Request,
    query_db: AsyncSession = Depends(get_db),
    user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    data = await Sys_statisticsService.add_visitor_guide_count_services(query_db, user.user.user_name)
    if data.is_success:
        logger.info('访客引导次数加1成功')
        return ResponseUtil.success(msg=data.message)
    else:
        logger.error('访客引导次数加1失败')
        return ResponseUtil.error(msg=data.message)
    
# 展厅讲解次数加1
@sys_statisticsController.get(
    '/add_explain_count'
)
async def add_explain_count(
    request: Request,
    query_db: AsyncSession = Depends(get_db),
    user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    data = await Sys_statisticsService.add_explain_count_services(query_db, user.user.user_name)
    if data.is_success:
        logger.info('展厅讲解次数加1成功')
        return ResponseUtil.success(msg=data.message)
    else:
        logger.error('展厅讲解次数加1失败')
        return ResponseUtil.error(msg=data.message)    

# @sys_statisticsController.get(
#     '/list', response_model=PageResponseModel
#     # , dependencies=[Depends(CheckUserInterfaceAuth('system:sys_statistics:list'))]
# )
# async def get_system_sys_statistics_list(
#     request: Request,
# sys_statistics_page_query: Sys_statisticsPageQueryModel = Depends(Sys_statisticsPageQueryModel.as_query),
#     query_db: AsyncSession = Depends(get_db),
# ):
#     # 获取分页数据
#     sys_statistics_page_query_result = await Sys_statisticsService.get_sys_statistics_list_services(query_db, sys_statistics_page_query, is_page=True)
#     logger.info('获取成功')
#
#     return ResponseUtil.success(model_content=sys_statistics_page_query_result)


# @sys_statisticsController.post(''
#     , dependencies=[Depends(CheckUserInterfaceAuth('system:sys_statistics:add'))]
#                                )
# @ValidateFields(validate_model='add_sys_statistics')
# @Log(title='系统统计数据', business_type=BusinessType.INSERT)
# async def add_system_sys_statistics(
#     request: Request,
#     add_sys_statistics: Sys_statisticsModel,
#     query_db: AsyncSession = Depends(get_db),
#     current_user: CurrentUserModel = Depends(LoginService.get_current_user),
# ):
#     add_sys_statistics.create_time = datetime.now()
#     add_sys_statistics.create_by = current_user.user.user_name
#     add_sys_statistics.update_time = datetime.now()
#     add_sys_statistics.update_by = current_user.user.user_name
#     add_sys_statistics_result = await Sys_statisticsService.add_sys_statistics_services(query_db, add_sys_statistics)
#     logger.info(add_sys_statistics_result.message)
#
#     return ResponseUtil.success(msg=add_sys_statistics_result.message)
#
#
# @sys_statisticsController.put('', dependencies=[Depends(CheckUserInterfaceAuth('system:sys_statistics:edit'))])
# @ValidateFields(validate_model='edit_sys_statistics')
# @Log(title='系统统计数据', business_type=BusinessType.UPDATE)
# async def edit_system_sys_statistics(
#     request: Request,
#     edit_sys_statistics: Sys_statisticsModel,
#     query_db: AsyncSession = Depends(get_db),
#     current_user: CurrentUserModel = Depends(LoginService.get_current_user),
# ):
#     edit_sys_statistics.update_by = current_user.user.user_name
#     edit_sys_statistics.update_time = datetime.now()
#     edit_sys_statistics_result = await Sys_statisticsService.edit_sys_statistics_services(query_db, edit_sys_statistics)
#     logger.info(edit_sys_statistics_result.message)
#
#     return ResponseUtil.success(msg=edit_sys_statistics_result.message)


# @sys_statisticsController.delete('/{statistic_ids}', dependencies=[Depends(CheckUserInterfaceAuth('system:sys_statistics:remove'))])
# @Log(title='系统统计数据', business_type=BusinessType.DELETE)
# async def delete_system_sys_statistics(request: Request, statistic_ids: str, query_db: AsyncSession = Depends(get_db)):
#     delete_sys_statistics = DeleteSys_statisticsModel(statisticIds=statistic_ids)
#     delete_sys_statistics_result = await Sys_statisticsService.delete_sys_statistics_services(query_db, delete_sys_statistics)
#     logger.info(delete_sys_statistics_result.message)
#
#     return ResponseUtil.success(msg=delete_sys_statistics_result.message)
#
#
# @sys_statisticsController.get(
#     '/{statistic_id}', response_model=Sys_statisticsModel, dependencies=[Depends(CheckUserInterfaceAuth('system:sys_statistics:query'))]
# )
# async def query_detail_system_sys_statistics(request: Request, statistic_id: int, query_db: AsyncSession = Depends(get_db)):
#     sys_statistics_detail_result = await Sys_statisticsService.sys_statistics_detail_services(query_db, statistic_id)
#     logger.info(f'获取statistic_id为{statistic_id}的信息成功')
#
#     return ResponseUtil.success(data=sys_statistics_detail_result)
#
#
# @sys_statisticsController.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('system:sys_statistics:export'))])
# @Log(title='系统统计数据', business_type=BusinessType.EXPORT)
# async def export_system_sys_statistics_list(
#     request: Request,
#     sys_statistics_page_query: Sys_statisticsPageQueryModel = Form(),
#     query_db: AsyncSession = Depends(get_db),
# ):
#     # 获取全量数据
#     sys_statistics_query_result = await Sys_statisticsService.get_sys_statistics_list_services(query_db, sys_statistics_page_query, is_page=False)
#     sys_statistics_export_result = await Sys_statisticsService.export_sys_statistics_list_services(sys_statistics_query_result)
#     logger.info('导出成功')
#
#     return ResponseUtil.streaming(data=bytes2file_response(sys_statistics_export_result))
