"""Tests for Sheet class."""

import pytest

from py_xmind16.sheet import Sheet
from py_xmind16.topic import Topic


class TestSheetCreation:
    """Tests for sheet creation."""
    
    def test_create_empty_sheet(self):
        """Test creating an empty sheet."""
        sheet = Sheet()
        assert sheet.title == "Sheet"
        assert sheet.id is not None
        assert len(sheet.id) == 32
    
    def test_create_sheet_with_title(self):
        """Test creating a sheet with a title."""
        sheet = Sheet(title="My Sheet")
        assert sheet.title == "My Sheet"
    
    def test_create_sheet_with_id(self):
        """Test creating a sheet with a specific ID."""
        custom_id = "custom123"
        sheet = Sheet(id=custom_id)
        assert sheet.id == custom_id
    
    def test_sheet_title_setter(self):
        """Test setting sheet title."""
        sheet = Sheet()
        sheet.title = "New Title"
        assert sheet.title == "New Title"


class TestSheetRootTopic:
    """Tests for sheet root topic operations."""
    
    def test_get_root_topic_creates_default(self):
        """Test get_root_topic creates a default root topic."""
        sheet = Sheet()
        root = sheet.get_root_topic()
        
        assert root is not None
        assert root.title == "Central Topic"
        assert sheet.root_topic == root
    
    def test_get_root_topic_returns_existing(self):
        """Test get_root_topic returns existing root topic."""
        sheet = Sheet()
        root1 = sheet.get_root_topic()
        root2 = sheet.get_root_topic()
        
        assert root1 is root2
    
    def test_set_root_topic(self):
        """Test setting a custom root topic."""
        sheet = Sheet()
        custom_root = Topic(title="Custom Root")
        sheet.set_root_topic(custom_root)
        
        assert sheet.root_topic == custom_root
    
    def test_initially_no_root_topic(self):
        """Test that sheet initially has no root topic."""
        sheet = Sheet()
        
        assert sheet.root_topic is None


class TestSheetSerialization:
    """Tests for sheet serialization."""
    
    def test_to_dict_basic(self):
        """Test basic sheet to dict conversion."""
        sheet = Sheet(title="Test", id="test123")
        data = sheet.to_dict()
        
        assert data["id"] == "test123"
        assert data["title"] == "Test"
        assert "rootTopic" not in data
    
    def test_to_dict_with_root_topic(self):
        """Test sheet with root topic to dict conversion."""
        sheet = Sheet(title="Test")
        root = sheet.get_root_topic()
        root.title = "My Root"
        data = sheet.to_dict()
        
        assert "rootTopic" in data
        assert data["rootTopic"]["title"] == "My Root"
    
    def test_from_dict_basic(self):
        """Test basic dict to sheet conversion."""
        data = {"id": "test123", "title": "Test"}
        sheet = Sheet.from_dict(data)
        
        assert sheet.id == "test123"
        assert sheet.title == "Test"
        assert sheet.root_topic is None
    
    def test_from_dict_with_root_topic(self):
        """Test dict with root topic to sheet conversion."""
        data = {
            "id": "sheet1",
            "title": "My Sheet",
            "rootTopic": {
                "id": "root1",
                "title": "Root Topic"
            }
        }
        sheet = Sheet.from_dict(data)
        
        assert sheet.root_topic is not None
        assert sheet.root_topic.title == "Root Topic"
    
    def test_round_trip_serialization(self, sheet_with_content):
        """Test round-trip serialization."""
        original = sheet_with_content
        data = original.to_dict()
        restored = Sheet.from_dict(data)
        
        assert restored.title == original.title
        assert restored.root_topic is not None
        assert restored.root_topic.title == original.root_topic.title
        assert len(restored.root_topic.subtopics) == len(original.root_topic.subtopics)


class TestSheetRepresentation:
    """Tests for sheet string representation."""
    
    def test_repr(self):
        """Test sheet representation."""
        sheet = Sheet(title="Test", id="1234567890abcdef")
        repr_str = repr(sheet)
        
        assert "Sheet" in repr_str
        assert "Test" in repr_str
