"""Tests for alive.network.conversation_log — activity logging."""

import tempfile
from pathlib import Path
from unittest.mock import patch

import pytest


@pytest.fixture
def tmp_log(tmp_path):
    log_path = tmp_path / "network" / "conversations.log"
    with patch("alive.network.conversation_log._log_path", return_value=log_path):
        yield log_path


class TestLogEvent:
    def test_creates_log_file(self, tmp_log):
        from alive.network.conversation_log import log_event

        log_event("DISCOVERED", "@friend at 192.168.1.50:8585")
        assert tmp_log.exists()

    def test_writes_formatted_entry(self, tmp_log):
        from alive.network.conversation_log import log_event

        log_event("SENT", "web_fetch to @friend (gen 1)")
        content = tmp_log.read_text()
        assert "SENT" in content
        assert "web_fetch" in content
        assert "\u2192" in content

    def test_appends_multiple_entries(self, tmp_log):
        from alive.network.conversation_log import log_event

        log_event("DISCOVERED", "peer 1")
        log_event("DISCOVERED", "peer 2")
        log_event("SENT", "skill to peer 1")
        lines = tmp_log.read_text().strip().splitlines()
        assert len(lines) == 3

    def test_creates_parent_dirs(self, tmp_path):
        from alive.network.conversation_log import log_event

        deep_path = tmp_path / "deep" / "nested" / "conversations.log"
        with patch("alive.network.conversation_log._log_path", return_value=deep_path):
            log_event("TEST", "deep path")
        assert deep_path.exists()

    def test_timestamp_format(self, tmp_log):
        from alive.network.conversation_log import log_event

        log_event("TEST", "timestamp check")
        content = tmp_log.read_text()
        # Should match [YYYY-MM-DD HH:MM:SS]
        assert content.startswith("[20")
        assert "]" in content


class TestReadLog:
    def test_empty_log(self, tmp_log):
        from alive.network.conversation_log import read_log

        entries = read_log()
        assert entries == []

    def test_reads_entries(self, tmp_log):
        from alive.network.conversation_log import log_event, read_log

        log_event("A", "first")
        log_event("B", "second")
        entries = read_log()
        assert len(entries) == 2
        assert "first" in entries[0]
        assert "second" in entries[1]

    def test_limits_lines(self, tmp_log):
        from alive.network.conversation_log import log_event, read_log

        for i in range(20):
            log_event("EVENT", f"entry {i}")
        entries = read_log(lines=5)
        assert len(entries) == 5
        # Should be the last 5
        assert "entry 15" in entries[0]
        assert "entry 19" in entries[4]

    def test_nonexistent_file(self, tmp_path):
        from alive.network.conversation_log import read_log

        with patch("alive.network.conversation_log._log_path", return_value=tmp_path / "nope.log"):
            entries = read_log()
        assert entries == []


class TestClearLog:
    def test_clears_log(self, tmp_log):
        from alive.network.conversation_log import log_event, clear_log, read_log

        log_event("A", "stuff")
        log_event("B", "more stuff")
        clear_log()
        entries = read_log()
        assert entries == []

    def test_clear_nonexistent(self, tmp_path):
        from alive.network.conversation_log import clear_log

        with patch("alive.network.conversation_log._log_path", return_value=tmp_path / "nope.log"):
            clear_log()  # Should not raise
