"""Phase 1 basic tests"""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent))

from src.capturers.base import RawNote, BaseCapturer
from src.db.schema import init_db, get_schema_version


class MockCapturer(BaseCapturer):
    def fetch(self): return []
    def get_last_synced(self): return None
    def update_registry(self, notes): pass


def test_raw_note():
    note = RawNote(
        source='memos',
        source_id='abc12345',
        content='test note content',
        created_at='2026-04-30T10:00:00+08:00',
    )
    assert note.source == 'memos'
    assert note.tags_raw == []
    print("test_raw_note OK")


def test_to_note_id():
    capturer = MockCapturer()
    note = RawNote('memos', 'abc12345', 'content', '2026-04-30T10:00:00+08:00')
    note_id = capturer.to_note_id(note)
    assert note_id == 'memos-20260430-abc12345', f"Got: {note_id}"
    print(f"test_to_note_id OK: {note_id}")


def test_filter_notes():
    capturer = MockCapturer()
    notes = [
        RawNote('memos', '1', 'short', '2026-04-30T10:00:00+08:00'),
        RawNote('memos', '2', 'valid note content here', '2026-04-30T10:00:00+08:00'),
        RawNote('memos', '3', 'excluded tag note', '2026-04-30T10:00:00+08:00', tags_raw=['#spam']),
    ]
    filtered = capturer.filter_notes(notes, min_length=10, exclude_tags=['#spam'])
    assert len(filtered) == 1 and filtered[0].source_id == '2'
    print("test_filter_notes OK")


def test_schema():
    import tempfile, os
    with tempfile.TemporaryDirectory() as tmpdir:
        db_path = Path(tmpdir) / "test.db"
        conn = init_db(db_path)
        schema = get_schema_version(conn)
        assert 'notes' in schema
        assert 'tagging_events' in schema
        assert 'vocabulary_events' in schema
        conn.close()
    print("test_schema OK")


if __name__ == '__main__':
    test_raw_note()
    test_to_note_id()
    test_filter_notes()
    test_schema()
    print("\n✅ All Phase 1 tests passed")
