from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession

from app.database import get_db
from app.models.user import User
from app.routers.auth import get_current_user
from app.schemas.brain import (
    BrainEventOut,
    BrainLearnRunOut,
    BrainMemoryOut,
    BrainOverviewOut,
    BrainTagGroupsOut,
)
from app.services.brain_service import BrainService

router = APIRouter(prefix="/api/brain", tags=["知识大脑"])


@router.get("/overview", response_model=BrainOverviewOut)
async def get_brain_overview(
    current_user: User = Depends(get_current_user),
    db: AsyncSession = Depends(get_db),
):
    service = BrainService(db)
    return await service.get_overview(current_user.id)


@router.get("/memories", response_model=list[BrainMemoryOut])
async def list_brain_memories(
    current_user: User = Depends(get_current_user),
    db: AsyncSession = Depends(get_db),
):
    service = BrainService(db)
    return await service.list_memories(current_user.id)


@router.get("/tags", response_model=BrainTagGroupsOut)
async def list_brain_tags(
    current_user: User = Depends(get_current_user),
    db: AsyncSession = Depends(get_db),
):
    service = BrainService(db)
    return await service.list_tags(current_user.id)


@router.get("/events", response_model=list[BrainEventOut])
async def list_brain_events(
    current_user: User = Depends(get_current_user),
    db: AsyncSession = Depends(get_db),
):
    service = BrainService(db)
    return await service.list_events(current_user.id)


@router.post("/learn/run", response_model=BrainLearnRunOut)
async def run_brain_learning(
    current_user: User = Depends(get_current_user),
    db: AsyncSession = Depends(get_db),
):
    service = BrainService(db)
    return await service.run_learning(current_user.id)
