from __future__ import annotations

from dataclasses import dataclass

from sqlalchemy import select
from sqlalchemy.orm import Session

from app.models.knowledge import Knowledge
from app.models.user import User


@dataclass(frozen=True)
class KnowledgeScope:
    id: int
    name: str
    feishu_space_id: str
    feishu_node_id: str
    is_mock: bool = False


class KnowledgeAccessService:
    @staticmethod
    def get_allowed_knowledge(db: Session, user: User) -> list[KnowledgeScope]:
        rows = db.scalars(select(Knowledge).where(Knowledge.status == 1).order_by(Knowledge.id.asc()))
        return [
            KnowledgeScope(
                id=knowledge.id,
                name=knowledge.name,
                feishu_space_id=knowledge.feishu_space_id,
                feishu_node_id=knowledge.feishu_node_id,
            )
            for knowledge in rows
        ]
