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.explanation_content_type_vo import Explanation_content_typePageQueryModel, \
    Explanation_content_typeQueryModel
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.explanation_content_type_service import Explanation_content_typeService
from module_admin.service.login_service import LoginService
from module_admin.service.explanation_content_service import Explanation_contentService
from module_admin.entity.vo.explanation_content_vo import DeleteExplanation_contentModel, Explanation_contentModel, Explanation_contentPageQueryModel
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 config.enums import ExplanationContentStatus


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


@explanationContentController.get(
    '/list', response_model=PageResponseModel
    # , dependencies=[Depends(CheckUserInterfaceAuth('system:explanation_content:list'))]
)
async def get_system_explanation_content_list(
    request: Request,
explanation_content_page_query: Explanation_contentPageQueryModel = Depends(Explanation_contentPageQueryModel.as_query),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取分页数据
    explanation_content_page_query_result = await Explanation_contentService.get_explanation_content_list_services(query_db, explanation_content_page_query, is_page=False)
    logger.info('获取成功')

    return ResponseUtil.success(data=explanation_content_page_query_result)


@explanationContentController.post('/add_explanation'
    # , dependencies=[Depends(CheckUserInterfaceAuth('system:explanation_content:add'))]
                                    )
@ValidateFields(validate_model='add_explanation_content')
@Log(title='讲解内容', business_type=BusinessType.INSERT)
async def add_system_explanation_content(
    request: Request,
    add_explanation_content: Explanation_contentModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    add_explanation_content.create_time = datetime.now()
    add_explanation_content.create_by = current_user.user.user_name
    add_explanation_content.update_time = datetime.now()
    add_explanation_content.update_by = current_user.user.user_name
    # 新添加内容状态默认0
    add_explanation_content.status = ExplanationContentStatus.NOTSTART.value
    add_explanation_content_result = await Explanation_contentService.add_explanation_content_services(query_db, add_explanation_content)
    logger.info(add_explanation_content_result.message)

    return ResponseUtil.success(msg=add_explanation_content_result.message)


@explanationContentController.put('/edit_explanation'
    # , dependencies=[Depends(CheckUserInterfaceAuth('system:explanation_content:edit'))]
                                   )
@ValidateFields(validate_model='edit_explanation_content')
@Log(title='讲解内容', business_type=BusinessType.UPDATE)
async def edit_system_explanation_content(
    request: Request,
    edit_explanation_content: Explanation_contentModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    edit_explanation_content.update_by = current_user.user.user_name
    edit_explanation_content.update_time = datetime.now()
    edit_explanation_content_result = await Explanation_contentService.edit_explanation_content_services(query_db, edit_explanation_content)
    logger.info(edit_explanation_content_result.message)

    return ResponseUtil.success(msg=edit_explanation_content_result.message)


@explanationContentController.delete('/{explanation_content_ids}'
    # , dependencies=[Depends(CheckUserInterfaceAuth('system:explanation_content:remove'))]
                                      )
@Log(title='讲解内容', business_type=BusinessType.DELETE)
async def delete_system_explanation_content(request: Request, explanation_content_ids: str, query_db: AsyncSession = Depends(get_db)):
    delete_explanation_content = DeleteExplanation_contentModel(explanationContentIds=explanation_content_ids)
    delete_explanation_content_result = await Explanation_contentService.delete_explanation_content_services(query_db, delete_explanation_content)
    logger.info(delete_explanation_content_result.message)

    return ResponseUtil.success(msg=delete_explanation_content_result.message)


@explanationContentController.get(
    '/type_list', response_model=PageResponseModel
    # , dependencies=[Depends(CheckUserInterfaceAuth('system:explanation_content_type:list'))]
)
async def get_system_explanation_content_type_list(
    request: Request,
# explanation_content_type_page_query: Explanation_content_typePageQueryModel = Depends(Explanation_content_typePageQueryModel.as_query),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取分页数据
    # explanation_content_type_page_query_result = await Explanation_content_typeService.get_explanation_content_type_list_services(query_db, explanation_content_type_page_query, is_page=True)

    explanation_content_type_query_result = await Explanation_content_typeService.get_explanation_content_type_list_services(query_db, Explanation_content_typeQueryModel(), is_page=False)

    logger.info('获取成功')

    return ResponseUtil.success(data=explanation_content_type_query_result)


# @explanationContentController.get(
#     '/{explanation_content_id}', response_model=Explanation_contentModel
#     # , dependencies=[Depends(CheckUserInterfaceAuth('system:explanation_content:query'))]
# )
# async def query_detail_system_explanation_content(request: Request, explanation_content_id: int, query_db: AsyncSession = Depends(get_db)):
#     explanation_content_detail_result = await Explanation_contentService.explanation_content_detail_services(query_db, explanation_content_id)
#     logger.info(f'获取explanation_content_id为{explanation_content_id}的信息成功')
#
#     return ResponseUtil.success(data=explanation_content_detail_result)


# @explanationContentController.post('/export'
#     # , dependencies=[Depends(CheckUserInterfaceAuth('system:explanation_content:export'))]
#                                     )
# @Log(title='讲解内容', business_type=BusinessType.EXPORT)
# async def export_system_explanation_content_list(
#     request: Request,
#     explanation_content_page_query: Explanation_contentPageQueryModel = Form(),
#     query_db: AsyncSession = Depends(get_db),
# ):
#     # 获取全量数据
#     explanation_content_query_result = await Explanation_contentService.get_explanation_content_list_services(query_db, explanation_content_page_query, is_page=False)
#     explanation_content_export_result = await Explanation_contentService.export_explanation_content_list_services(explanation_content_query_result)
#     logger.info('导出成功')
#
#     return ResponseUtil.streaming(data=bytes2file_response(explanation_content_export_result))
