class Explanation: ...


class SelectorsBase:
    @classmethod
    def add_confirmed_filter(cls, first_table, second_table) -> tuple:
        return (
            first_table.active == True,
            first_table.is_confirmed == True,
            first_table.deleted == False,
            second_table.active == True,
            second_table.is_confirmed == True,
            second_table.deleted == False,
        )


class SelectActionWithEmployee:

    @classmethod
    def select_action(cls, employee_id, filter_expr: list = None):
        if filter_expr is not None:
            filter_expr = (cls.__many__table__.employee_id == employee_id, *filter_expr)
            data = (
                cls.session.query(cls.id)
                .select_from(cls)
                .join(cls.__many__table__, cls.__many__table__.member_id == cls.id)
                .filter(
                    *filter_expr,
                    *SelectorsBase.add_confirmed_filter(
                        first_table=cls, second_table=cls.__many__table__
                    ),
                )
            )
            return cls.query.filter(cls.id.in_([comp[0] for comp in data.all()]))
        data = (
            cls.session.query(cls.id)
            .select_from(cls)
            .join(cls.__many__table__, cls.__many__table__.member_id == cls.id)
            .filter(
                cls.__many__table__.employee_id == employee_id,
                *SelectorsBase.add_confirmed_filter(
                    first_table=cls, second_table=cls.__many__table__
                ),
            )
        )
        return cls.query.filter(cls.id.in_([comp[0] for comp in data.all()]))


class SelectAction:

    @classmethod
    def select_action(cls, duty_id_list: list, filter_expr: list = None):
        if filter_expr is not None:
            data = (
                cls.session.query(cls.id)
                .select_from(cls)
                .join(cls.__many__table__, cls.__many__table__.member_id == cls.id)
                .filter(
                    cls.__many__table__.duties_id.in_(duty_id_list),
                    *SelectorsBase.add_confirmed_filter(
                        first_table=cls, second_table=cls.__many__table__
                    ),
                    *filter_expr,
                )
            )
            return cls.query.filter(cls.id.in_([comp[0] for comp in data.all()]))

        data = (
            cls.session.query(cls.id)
            .select_from(cls)
            .join(cls.__many__table__, cls.__many__table__.member_id == cls.id)
            .filter(
                cls.__many__table__.duties_id.in_(duty_id_list),
                *SelectorsBase.add_confirmed_filter(
                    first_table=cls, second_table=cls.__many__table__
                ),
            )
        )
        return cls.query.filter(cls.id.in_([comp[0] for comp in data.all()]))
