from datetime import datetime, time
from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession

from module_admin.entity.do.explanation_style_do import ExplanationStyle
from module_admin.entity.do.explanation_style_robot_pair_do import ExplanationStyleRobotPairing
from module_admin.entity.do.explanation_style_do import ExplanationStyle
from module_admin.entity.vo.explanation_style_robot_pair_vo import Explanation_style_robot_pairModel, \
    Explanation_style_robot_pairPageQueryModel, SwitchExplanationStyleModel
from utils.page_util import PageUtil


class Explanation_style_robot_pairDao:
    """
    讲解风格--机器人配对模块数据库操作层
    """
    
    @classmethod
    async def get_robot_role_style(cls, db: AsyncSession, robot_id: int, role_id: int):
        """根据机器人id和角色id获取机器人角色样式

        Args:
            db (AsyncSession): 数据库会话
            robot_id (int): 机器人id
            role_id (int): 角色id

        Returns:
            _type_: _description_
        """

        stmt = (
            select(ExplanationStyleRobotPairing.pairing_id, ExplanationStyleRobotPairing.explanation_style_id, ExplanationStyle.name, ExplanationStyle.detail, ExplanationStyleRobotPairing.status, ExplanationStyleRobotPairing.robot_role_id)
            .select_from(ExplanationStyleRobotPairing)
            .join(ExplanationStyle, ExplanationStyle.explanation_style_id == ExplanationStyleRobotPairing.explanation_style_id)
            .where(ExplanationStyleRobotPairing.robot_id == robot_id, ExplanationStyleRobotPairing.robot_role_id == role_id)
        )
        
        result = (await db.execute(stmt)).mappings().all()
        return result
    
    

    @classmethod
    async def switch_explanation_style(cls, db: AsyncSession, robot_id: int = None, pairing_id: int = None, robot_role_id: str = None ,status:str = '0', update_by: str = None):

        condition = list()

        if robot_id:
            condition.append(ExplanationStyleRobotPairing.robot_id == robot_id)
        if pairing_id:
            condition.append(ExplanationStyleRobotPairing.pairing_id == pairing_id)
        if robot_role_id:
            condition.append(ExplanationStyleRobotPairing.robot_role_id == robot_role_id)

        query = (
            update(ExplanationStyleRobotPairing)
            .values(status=status)
            .where(*condition)
        )
        return await db.execute(query)

    @classmethod
    async def get_style_robot_pair_list(cls, db: AsyncSession, robot_id: int):
        query = (
            select(ExplanationStyleRobotPairing.pairing_id, ExplanationStyle.name, ExplanationStyle.detail, ExplanationStyleRobotPairing.status)
            .select_from(ExplanationStyleRobotPairing)
            .join(ExplanationStyle, ExplanationStyle.explanation_style_id == ExplanationStyleRobotPairing.explanation_style_id)
            .where(ExplanationStyleRobotPairing.robot_id == robot_id)
        )

        result = (await db.execute(query)).mappings().all()
        return result


    @classmethod
    async def get_explanation_style_robot_pair_detail_by_id(cls, db: AsyncSession, pairing_id: int):
        """
        根据主键ID获取讲解风格--机器人配对详细信息

        :param db: orm对象
        :param pairing_id: 主键ID
        :return: 讲解风格--机器人配对信息对象
        """
        explanation_style_robot_pair_info = (
            (
                await db.execute(
                    select(ExplanationStyleRobotPairing)
                    .where(
                        ExplanationStyleRobotPairing.pairing_id == pairing_id
                    )
                )
            )
            .scalars()
            .first()
        )

        return explanation_style_robot_pair_info

    @classmethod
    async def get_explanation_style_robot_pair_detail_by_info(cls, db: AsyncSession, explanation_style_robot_pair: Explanation_style_robot_pairModel):
        """
        根据讲解风格--机器人配对参数获取讲解风格--机器人配对信息

        :param db: orm对象
        :param explanation_style_robot_pair: 讲解风格--机器人配对参数对象
        :return: 讲解风格--机器人配对信息对象
        """
        explanation_style_robot_pair_info = (
            (
                await db.execute(
                    select(ExplanationStyleRobotPairing).where(
                    )
                )
            )
            .scalars()
            .first()
        )

        return explanation_style_robot_pair_info

    @classmethod
    async def get_explanation_style_robot_pair_list(cls, db: AsyncSession, query_object: Explanation_style_robot_pairPageQueryModel, is_page: bool = False):
        """
        根据查询参数获取讲解风格--机器人配对列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 讲解风格--机器人配对列表信息对象
        """
        query = (
            select(ExplanationStyleRobotPairing)
            .where(
                ExplanationStyleRobotPairing.robot_id == query_object.robot_id if query_object.robot_id else True,
                ExplanationStyleRobotPairing.explanation_style_id == query_object.explanation_style_id if query_object.explanation_style_id else True,
                ExplanationStyleRobotPairing.prompt == query_object.prompt if query_object.prompt else True,
                ExplanationStyleRobotPairing.status == query_object.status if query_object.status else True,
                ExplanationStyleRobotPairing.create_time.between(
                    datetime.combine(datetime.strptime(query_object.begin_create_time, '%Y-%m-%d'), time(00, 00, 00)),
                    datetime.combine(datetime.strptime(query_object.end_create_time, '%Y-%m-%d'), time(23, 59, 59)),
                )
                if query_object.begin_create_time and query_object.end_create_time
                else True,
                ExplanationStyleRobotPairing.update_time.between(
                    datetime.combine(datetime.strptime(query_object.begin_update_time, '%Y-%m-%d'), time(00, 00, 00)),
                    datetime.combine(datetime.strptime(query_object.end_update_time, '%Y-%m-%d'), time(23, 59, 59)),
                )
                if query_object.begin_update_time and query_object.end_update_time
                else True,
            )
            .order_by(ExplanationStyleRobotPairing.pairing_id)
            .distinct()
        )
        explanation_style_robot_pair_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return explanation_style_robot_pair_list

    @classmethod
    async def add_explanation_style_robot_pair_dao(cls, db: AsyncSession, explanation_style_robot_pair: Explanation_style_robot_pairModel):
        """
        新增讲解风格--机器人配对数据库操作

        :param db: orm对象
        :param explanation_style_robot_pair: 讲解风格--机器人配对对象
        :return:
        """
        db_explanation_style_robot_pair = ExplanationStyleRobotPairing(**explanation_style_robot_pair.model_dump(exclude={}))
        db.add(db_explanation_style_robot_pair)
        await db.flush()

        return db_explanation_style_robot_pair

    @classmethod
    async def edit_explanation_style_robot_pair_dao(cls, db: AsyncSession, explanation_style_robot_pair: dict):
        """
        编辑讲解风格--机器人配对数据库操作

        :param db: orm对象
        :param explanation_style_robot_pair: 需要更新的讲解风格--机器人配对字典
        :return:
        """
        await db.execute(update(ExplanationStyleRobotPairing), [explanation_style_robot_pair])

    @classmethod
    async def delete_explanation_style_robot_pair_dao(cls, db: AsyncSession, explanation_style_robot_pair: Explanation_style_robot_pairModel):
        """
        删除讲解风格--机器人配对数据库操作

        :param db: orm对象
        :param explanation_style_robot_pair: 讲解风格--机器人配对对象
        :return:
        """
        await db.execute(delete(ExplanationStyleRobotPairing).where(ExplanationStyleRobotPairing.pairing_id.in_([explanation_style_robot_pair.pairing_id])))

