from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session
from app.database import get_db
from app.models.user import User
from app.routers.auth import get_current_user
from app.schemas.stats import (
    SystemHealth,
    ConversationStats,
    KnowledgeStats,
    KanbanStats,
    CommunityStats,
    PersonalInsights,
)
from app.services.stats_service import StatsService

router = APIRouter(prefix="/api/stats", tags=["统计"])


@router.get("/system", response_model=SystemHealth)
async def get_system_health(db: Session = Depends(get_db)):
    """获取系统健康指标"""
    svc = StatsService(db)
    return svc.get_system_health()


@router.get("/conversations", response_model=ConversationStats)
async def get_conversation_stats(
    days: int = 30,
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db),
):
    """获取对话统计数据"""
    svc = StatsService(db)
    return svc.get_conversation_stats(user_id=current_user.id, days=days)


@router.get("/knowledge", response_model=KnowledgeStats)
async def get_knowledge_stats(
    days: int = 30,
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db),
):
    """获取知识库统计数据"""
    svc = StatsService(db)
    return svc.get_knowledge_stats(user_id=current_user.id, days=days)


@router.get("/kanban", response_model=KanbanStats)
async def get_kanban_stats(
    days: int = 30,
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db),
):
    """获取看板统计数据"""
    svc = StatsService(db)
    return svc.get_kanban_stats(user_id=current_user.id, days=days)


@router.get("/community", response_model=CommunityStats)
async def get_community_stats(
    days: int = 30,
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db),
):
    """获取社区统计数据"""
    svc = StatsService(db)
    return svc.get_community_stats(user_id=current_user.id, days=days)


@router.get("/insights", response_model=PersonalInsights)
async def get_personal_insights(
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db),
):
    """获取个人洞察"""
    svc = StatsService(db)
    return svc.get_personal_insights(user_id=current_user.id)
