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.user_vo import CurrentUserModel
from module_admin.service.login_service import LoginService
from module_admin.service.visitor_type_service import Visitor_typeService
from module_admin.service.words_service import WordsService
from module_admin.entity.vo.words_vo import DeleteWordsModel, WordsModel, WordsPageQueryModel, WordsUpdateModel
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



wordsController = APIRouter(prefix='/guide/words', dependencies=[Depends(LoginService.get_current_user)])


# 随机获取引导词
@wordsController.get('/random_words/{type}')
async def get_random_guide_word(request: Request, type: str, query_db: AsyncSession = Depends(get_db)):
    
    guide_word = await WordsService.get_random_guide_word_services(query_db, type)
    return ResponseUtil.success(data=guide_word)
    


@wordsController.post(
    '/list', response_model=PageResponseModel
    # , dependencies=[Depends(CheckUserInterfaceAuth('guide:words:list'))]
)
async def get_guide_words_list(
    request: Request,
    words_page_query: WordsPageQueryModel = Depends(WordsPageQueryModel.as_query),
    query_db: AsyncSession = Depends(get_db),
):
    print(words_page_query.begin_time)
    print(words_page_query.end_time)
    # 获取分页数据
    words_page_query_result = await WordsService.get_words_list_services(query_db, words_page_query, is_page=True)
    logger.info('获取成功')

    return ResponseUtil.success(model_content=words_page_query_result)


#批量启用引导词
@wordsController.post('/enable_guide/{guide_ids}',)
async def enable_guide(
    request: Request,
    guide_ids: str,
    query_db: AsyncSession = Depends(get_db),

):
    edit_del_result = await WordsService.edit_del_words_services(query_db=query_db, guide_ids=guide_ids, del_flg='0')
    return ResponseUtil.success(msg=edit_del_result.message)

#批量禁用引导词
@wordsController.post('/disable_guide/{guide_ids}',)
async def disable_guide(
    request: Request,
    guide_ids: str,
    query_db: AsyncSession = Depends(get_db),

):
    edit_del_result = await WordsService.edit_del_words_services(query_db=query_db, guide_ids=guide_ids, del_flg='2')
    return ResponseUtil.success(msg=edit_del_result.message)

#获取访客类型列表
@wordsController.get('/visitor_type_list',)
async def get_system_visitor_type_list(
    request: Request,
    query_db: AsyncSession = Depends(get_db),
):
    visitor_type_list = await Visitor_typeService.get_system_visitor_type_list(query_db=query_db)
    return ResponseUtil.success(data=visitor_type_list)



@wordsController.post('/add_guide'
    # , dependencies=[Depends(CheckUserInterfaceAuth('guide:words:add'))]
                      )
@ValidateFields(validate_model='add_words')
@Log(title='引导词', business_type=BusinessType.INSERT)
async def add_guide_words(
    request: Request,
    add_words: WordsModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    if add_words.type is None:
        return ResponseUtil.failure(msg='引导词类型不能为空')
    if add_words.text is None:
        return ResponseUtil.failure(msg='引导词内容不能为空')
    add_words.create_time = datetime.now()
    add_words.update_time = datetime.now()
    add_words.create_by = current_user.user.user_name
    add_words.del_flag = "0"
    add_words_result = await WordsService.add_words_services(query_db, add_words)
    logger.info(add_words_result.message)

    return ResponseUtil.success(msg=add_words_result.message)


@wordsController.put('/edit_guide'
    # ,dependencies=[Depends(CheckUserInterfaceAuth('guide:words:edit'))]
                     )
@ValidateFields(validate_model='edit_words')
@Log(title='引导词', business_type=BusinessType.UPDATE)
async def edit_guide_words(
    request: Request,
    edit_words: WordsUpdateModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):

    edit_words.update_by = current_user.user.user_name
    edit_words.update_time = datetime.now()
    edit_words_result = await WordsService.edit_words_services(query_db, edit_words)
    logger.info(edit_words_result.message)

    return ResponseUtil.success(msg=edit_words_result.message)


@wordsController.delete('/{guide_ids}'
    # , dependencies=[Depends(CheckUserInterfaceAuth('guide:words:remove'))]
                        )
@Log(title='引导词', business_type=BusinessType.DELETE)
async def delete_guide_words(request: Request, guide_ids: str, query_db: AsyncSession = Depends(get_db)):
    delete_words = DeleteWordsModel(guideIds=guide_ids)
    delete_words_result = await WordsService.delete_words_services(query_db, delete_words)
    logger.info(delete_words_result.message)

    return ResponseUtil.success(msg=delete_words_result.message)


@wordsController.get(
    '/{guide_id}', response_model=WordsModel
    # , dependencies=[Depends(CheckUserInterfaceAuth('guide:words:query'))]
)
async def query_detail_guide_words(request: Request, guide_id: int, query_db: AsyncSession = Depends(get_db)):
    words_detail_result = await WordsService.words_detail_services(query_db, guide_id)
    logger.info(f'获取guide_id为{guide_id}的信息成功')

    return ResponseUtil.success(data=words_detail_result)


@wordsController.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('guide:words:export'))])
@Log(title='引导词', business_type=BusinessType.EXPORT)
async def export_guide_words_list(
    request: Request,
    words_page_query: WordsPageQueryModel = Form(),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取全量数据
    words_query_result = await WordsService.get_words_list_services(query_db, words_page_query, is_page=False)
    words_export_result = await WordsService.export_words_list_services(words_query_result)
    logger.info('导出成功')

    return ResponseUtil.streaming(data=bytes2file_response(words_export_result))
