from sqlalchemy.orm import Session
from sqlalchemy import func
from app.models.chat_history import ChatHistory

class MISService:

    def summary(self, db: Session):
        total = db.query(func.count(ChatHistory.id)).scalar()

        categories = db.query(
            ChatHistory.detected_category,
            func.count(ChatHistory.id)
        ).group_by(ChatHistory.detected_category).all()

        recent = db.query(ChatHistory).order_by(ChatHistory.id.desc()).limit(10).all()

        return {
            "total": total,
            "categories": categories,
            "recent": recent
        }