from typing import Optional

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

from app.database import get_session
from app.crud import activity as crud_activity
from app.schemas.activity import PaginatedActivityResponse

router = APIRouter()

@router.get(
    "/groups/{group_id}/activity",
    response_model=PaginatedActivityResponse,
    summary="Get group activity feed",
    tags=["Activity"],
)
async def get_group_activity(
    group_id: int,
    limit: int = Query(20, ge=1, le=100),
    cursor: Optional[int] = Query(None, description="Unix timestamp for pagination"),
    db: AsyncSession = Depends(get_session),
):
    """
    Retrieves a paginated feed of recent activities within a group.
    """
    activities = await crud_activity.get_group_activity(db, group_id=group_id, limit=limit, cursor=cursor)
    
    next_cursor = None
    if activities and len(activities) == limit:
        next_cursor = int(activities[-1].timestamp.timestamp())

    return PaginatedActivityResponse(items=activities, cursor=next_cursor) 