"""Tests for temporal.core.models — Message, ContentBlock, SummaryRecord."""

from datetime import date, datetime, timezone

from temporal import ContentBlock, Message, SummaryRecord


class TestContentBlock:
    def test_text_block_display(self):
        block = ContentBlock(type="text", text="Hello world")
        assert block.display_text() == "Hello world"

    def test_tool_call_display(self):
        block = ContentBlock(type="tool_call", tool_name="search", tool_args={"q": "test"})
        assert "search" in block.display_text()
        assert "test" in block.display_text()

    def test_tool_result_display(self):
        block = ContentBlock(type="tool_result", tool_output="Found 5 results")
        assert block.display_text() == "Found 5 results"

    def test_empty_block_display(self):
        block = ContentBlock(type="text", text=None)
        assert block.display_text() == ""


class TestMessage:
    def test_string_content(self):
        msg = Message(role="user", content="Hello")
        assert msg.plain_text() == "Hello"

    def test_block_content(self):
        msg = Message(
            role="assistant",
            content=[
                ContentBlock(type="text", text="Let me search."),
                ContentBlock(type="tool_call", tool_name="search", tool_args={"q": "test"}),
            ],
        )
        text = msg.plain_text()
        assert "Let me search." in text
        assert "search" in text

    def test_to_dict_string_content(self):
        msg = Message(role="user", content="Hello")
        d = msg.to_dict()
        assert d == {"role": "user", "content": "Hello"}

    def test_to_dict_block_content(self):
        msg = Message(
            role="assistant",
            content=[ContentBlock(type="text", text="Hi")],
        )
        d = msg.to_dict()
        assert d["role"] == "assistant"
        assert isinstance(d["content"], list)
        assert d["content"][0]["type"] == "text"
        assert d["content"][0]["text"] == "Hi"

    def test_to_dict_with_timestamp(self):
        ts = datetime(2026, 3, 2, 10, 0, 0, tzinfo=timezone.utc)
        msg = Message(role="user", content="Hi", timestamp=ts)
        d = msg.to_dict()
        assert "timestamp" in d

    def test_from_openai_simple(self):
        msg = Message.from_openai({"role": "user", "content": "Hello"})
        assert msg.role == "user"
        assert msg.content == "Hello"

    def test_from_openai_with_tool_calls(self):
        msg = Message.from_openai({
            "role": "assistant",
            "content": None,
            "tool_calls": [
                {
                    "id": "call_1",
                    "function": {"name": "search", "arguments": '{"q": "test"}'},
                }
            ],
        })
        assert msg.role == "assistant"
        assert isinstance(msg.content, list)
        tool_blocks = [b for b in msg.content if b.type == "tool_call"]
        assert len(tool_blocks) == 1
        assert tool_blocks[0].tool_name == "search"
        assert tool_blocks[0].tool_args == {"q": "test"}

    def test_from_openai_tool_result(self):
        msg = Message.from_openai({
            "role": "tool",
            "content": "Found 5 results",
            "tool_call_id": "call_1",
        })
        assert isinstance(msg.content, list)
        assert msg.content[0].type == "tool_result"
        assert msg.content[0].tool_output == "Found 5 results"

    def test_from_dict(self):
        msg = Message.from_dict({"role": "assistant", "content": "Hi there"})
        assert msg.role == "assistant"
        assert msg.content == "Hi there"

    def test_from_dict_defaults(self):
        msg = Message.from_dict({})
        assert msg.role == "user"
        assert msg.content == ""


class TestSummaryRecord:
    def test_five_minute_fields(self):
        sr = SummaryRecord(
            summary="test", message_count=5,
            date=date(2026, 3, 2), hour=10, minute=5,
        )
        assert sr.date == date(2026, 3, 2)
        assert sr.hour == 10
        assert sr.minute == 5

    def test_weekly_fields(self):
        sr = SummaryRecord(
            summary="weekly", message_count=50,
            week_start_date=date(2026, 2, 22),
        )
        assert sr.week_start_date == date(2026, 2, 22)

    def test_monthly_fields(self):
        sr = SummaryRecord(summary="monthly", message_count=200, year=2026, month=2)
        assert sr.year == 2026
        assert sr.month == 2

    def test_optional_fields_default_none(self):
        sr = SummaryRecord(summary="basic", message_count=1)
        assert sr.date is None
        assert sr.hour is None
        assert sr.minute is None
        assert sr.week_start_date is None
        assert sr.year is None
        assert sr.month is None
