"""知识库文章 API — P1-3 嵌入功能模块用

提供按关联页面查询知识文章的功能。
"""
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy.orm import Session
from typing import Optional
from app.database import get_db
from app.auth_middleware import require_role
from app.models.knowledge_article import KnowledgeArticle

router = APIRouter(prefix="/api/cma/knowledge-articles", tags=["知识库嵌入"],
    dependencies=[Depends(require_role("ceo", "finance", "business", "it"))],
)


def article_to_dict(a: KnowledgeArticle) -> dict:
    return {
        "id": a.id,
        "title": a.title,
        "summary": a.summary,
        "content": a.content,
        "category": a.category,
        "icon": a.icon,
        "related_page": a.related_page,
        "sort_order": a.sort_order,
    }


@router.get("")
def list_articles(
    related_page: Optional[str] = Query(None, description="按关联页面路由筛选"),
    category: Optional[str] = Query(None),
    db: Session = Depends(get_db),
):
    """查询知识文章，可按关联页面或分类筛选"""
    q = db.query(KnowledgeArticle)
    if related_page:
        q = q.filter(KnowledgeArticle.related_page.contains(related_page))
    if category:
        q = q.filter(KnowledgeArticle.category == category)
    articles = q.order_by(KnowledgeArticle.sort_order.asc(), KnowledgeArticle.id.asc()).all()
    return {"data": [article_to_dict(a) for a in articles]}


@router.get("/{article_id}")
def get_article(article_id: int, db: Session = Depends(get_db)):
    a = db.query(KnowledgeArticle).filter(KnowledgeArticle.id == article_id).first()
    if not a:
        raise HTTPException(404, "文章不存在")
    return article_to_dict(a)
