"""Timeline route."""

from __future__ import annotations

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

from server.db import repositories as repo
from server.db.engine import get_session
from server.schemas.responses import EpisodeResponse, MemoryResponse, TimelineResponse
from server.core.dependencies import get_tenant_id

router = APIRouter(tags=["timeline"])


@router.get("/v1/timeline", response_model=TimelineResponse, summary="Get subject timeline")
async def get_timeline(
    subject_id: str = Query(...),
    session: AsyncSession = Depends(get_session),
    tenant_id: str | None = Depends(get_tenant_id),
):
    episodes = await repo.list_episodes_by_subject(session, subject_id, tenant_id=tenant_id)
    memories = await repo.list_memories_by_subject(session, subject_id, tenant_id=tenant_id)
    return TimelineResponse(
        subject_id=subject_id,
        episodes=[
            EpisodeResponse(
                id=e.id,
                subject_id=e.subject_id,
                source=e.source,
                type=e.type,
                payload=e.payload,
                metadata=e.metadata_,
                provenance=e.provenance,
                session_id=e.session_id,
                occurred_at=e.occurred_at,
                created_at=e.created_at,
            )
            for e in episodes
        ],
        memories=[
            MemoryResponse(
                id=m.id,
                subject_id=m.subject_id,
                kind=m.kind,
                content=m.content,
                summary=m.summary,
                confidence=m.confidence,
                valid_from=m.valid_from,
                valid_to=m.valid_to,
                source_episode_ids=m.source_episode_ids or [],
                metadata=m.metadata_,
                status=m.status,
                sensitivity_labels=list(m.sensitivity_labels or []),
                created_at=m.created_at,
                updated_at=m.updated_at,
            )
            for m in memories
        ],
    )
