"""Tests for models module."""

import pytest
from datetime import datetime
from anki_sync.models import Note


class TestNote:
    """Tests for the Note class."""
    
    def test_note_creation(self):
        """Test creating a Note."""
        note = Note(
            id=123,
            type="Basic",
            tags=["tag1", "tag2"],
            fields={"Front": "Question", "Back": "Answer"}
        )
        
        assert note.id == 123
        assert note.type == "Basic"
        assert note.tags == ["tag1", "tag2"]
        assert note.fields == {"Front": "Question", "Back": "Answer"}
    
    def test_note_to_dict(self):
        """Test converting note to dictionary."""
        note = Note(
            id=456,
            type="Cloze",
            fields={"Text": "This is {{c1::important}}"}
        )
        
        note_dict = note.to_dict()
        assert note_dict['id'] == 456
        assert note_dict['type'] == "Cloze"
        assert note_dict['fields']['Text'] == "This is {{c1::important}}"
    
    def test_note_from_dict(self):
        """Test creating note from dictionary."""
        data = {
            'id': 789,
            'type': 'Basic',
            'tags': ['test'],
            'fields': {'Front': 'Q', 'Back': 'A'},
            'created': '2024-02-12T10:00:00',
            'modified': '2024-02-12T10:00:00'
        }
        
        note = Note.from_dict(data)
        assert note.id == 789
        assert note.type == 'Basic'
        assert 'test' in note.tags
    
    def test_note_yaml_roundtrip(self):
        """Test YAML serialization and deserialization."""
        original = Note(
            id=111,
            type="Basic",
            tags=["yaml", "test"],
            fields={"Front": "Q1", "Back": "A1"}
        )
        
        yaml_str = original.to_yaml()
        loaded = Note.from_yaml(yaml_str)
        
        assert loaded.id == original.id
        assert loaded.type == original.type
        assert loaded.tags == original.tags
        assert loaded.fields == original.fields
    
    def test_note_get_summary(self):
        """Test getting note summary."""
        note = Note(
            id=1,
            type="Basic",
            fields={"Front": "This is a long question that should be truncated", "Back": "Answer"}
        )
        
        summary = note.get_summary()
        assert "is a long" in summary
        assert len(summary) <= 50
    
    def test_note_empty_fields_summary(self):
        """Test summary with empty fields."""
        note = Note(
            id=2,
            type="Basic",
            fields={"Front": "", "Back": ""}
        )
        
        summary = note.get_summary()
        assert summary == "note"
