from datetime import UTC, datetime

from dana.api.core.schemas import (
    ConversationBase,
    ConversationCreate,
    ConversationRead,
    ConversationWithMessages,
    MessageBase,
    MessageCreate,
    MessageRead,
)


def test_conversation_base_schema():
    data = {"title": "Chat 1", "agent_id": 1}
    convo = ConversationBase(**data)
    assert convo.title == "Chat 1"
    assert convo.agent_id == 1


def test_conversation_create_schema():
    data = {"title": "Chat 2", "agent_id": 2}
    convo = ConversationCreate(**data)
    assert convo.title == "Chat 2"
    assert convo.agent_id == 2


def test_conversation_read_schema():
    now = datetime.now(UTC)
    data = {"id": 1, "title": "Chat 3", "agent_id": 3, "created_at": now, "updated_at": now}
    convo = ConversationRead(**data)
    assert convo.id == 1
    assert convo.title == "Chat 3"
    assert convo.agent_id == 3
    assert convo.created_at == now
    assert convo.updated_at == now


def test_message_base_schema():
    data = {"sender": "user", "content": "Hello!"}
    msg = MessageBase(**data)
    assert msg.sender == "user"
    assert msg.content == "Hello!"


def test_message_create_schema():
    data = {"sender": "bot", "content": "Hi!"}
    msg = MessageCreate(**data)
    assert msg.sender == "bot"
    assert msg.content == "Hi!"


def test_message_read_schema():
    now = datetime.now(UTC)
    data = {"id": 1, "conversation_id": 2, "sender": "user", "content": "Hello!", "created_at": now, "updated_at": now}
    msg = MessageRead(**data)
    assert msg.id == 1
    assert msg.conversation_id == 2
    assert msg.sender == "user"
    assert msg.content == "Hello!"
    assert msg.created_at == now
    assert msg.updated_at == now


def test_conversation_with_messages_schema():
    now = datetime.now(UTC)
    convo_data = {
        "id": 1,
        "title": "Chat",
        "agent_id": 4,
        "created_at": now,
        "updated_at": now,
        "messages": [{"id": 1, "conversation_id": 1, "sender": "user", "content": "Hi", "created_at": now, "updated_at": now}],
    }
    convo = ConversationWithMessages(**convo_data)
    assert convo.id == 1
    assert convo.title == "Chat"
    assert convo.agent_id == 4
    assert convo.created_at == now
    assert convo.updated_at == now
    assert len(convo.messages) == 1
    assert convo.messages[0].sender == "user"
