from __future__ import annotations

from datetime import datetime, timezone
from decimal import Decimal
from uuid import UUID

from models.agent_chat_session import AgentChatSession, AgentChatSessionStatus
from v1.agent.service import select_recent_session


def test_select_recent_session_uses_last_activity_desc() -> None:
    sessions = [
        AgentChatSession(
            id=UUID("00000000-0000-0000-0000-000000000001"),
            user_id=UUID("00000000-0000-0000-0000-0000000000a1"),
            title="older",
            status=AgentChatSessionStatus.COMPLETED,
            last_activity_at=datetime(2026, 2, 25, 9, 0, tzinfo=timezone.utc),
            message_count=1,
            total_tokens=1,
            total_cost=Decimal("0"),
        ),
        AgentChatSession(
            id=UUID("00000000-0000-0000-0000-000000000002"),
            user_id=UUID("00000000-0000-0000-0000-0000000000a1"),
            title="newer",
            status=AgentChatSessionStatus.RUNNING,
            last_activity_at=datetime(2026, 2, 25, 10, 0, tzinfo=timezone.utc),
            message_count=2,
            total_tokens=2,
            total_cost=Decimal("0"),
        ),
    ]

    selected = select_recent_session(sessions)

    assert selected is not None
    assert selected.id == UUID("00000000-0000-0000-0000-000000000002")


def test_select_recent_session_returns_none_for_empty_collection() -> None:
    assert select_recent_session([]) is None
