#!/usr/bin/env python3
# Copyright (c) 2025 Patrick Motsch
# All rights reserved.
"""
Unit tests for document reference models in datamodelDocref.py
Tests DocumentReference, DocumentListReference, DocumentItemReference, DocumentReferenceList.
"""

import pytest

from modules.datamodels.datamodelDocref import (
    DocumentReference,
    DocumentListReference,
    DocumentItemReference,
    DocumentReferenceList
)


class TestDocumentListReference:
    """Test DocumentListReference model"""
    
    def test_documentListReference_creation(self):
        """Test creating DocumentListReference with label only"""
        ref = DocumentListReference(label="task1_results")
        assert ref.label == "task1_results"
        assert ref.messageId is None
    
    def test_documentListReference_with_messageId(self):
        """Test DocumentListReference with messageId"""
        ref = DocumentListReference(
            messageId="msg123",
            label="task1_results"
        )
        assert ref.messageId == "msg123"
        assert ref.label == "task1_results"
    
    def test_documentListReference_to_string(self):
        """Test to_string() method"""
        ref = DocumentListReference(label="task1_results")
        assert ref.to_string() == "docList:task1_results"
        
        ref = DocumentListReference(messageId="msg123", label="task1_results")
        assert ref.to_string() == "docList:msg123:task1_results"


class TestDocumentItemReference:
    """Test DocumentItemReference model"""
    
    def test_documentItemReference_creation(self):
        """Test creating DocumentItemReference"""
        ref = DocumentItemReference(documentId="doc123")
        assert ref.documentId == "doc123"
        assert ref.fileName is None
    
    def test_documentItemReference_with_filename(self):
        """Test DocumentItemReference with fileName"""
        ref = DocumentItemReference(
            documentId="doc123",
            fileName="test.pdf"
        )
        assert ref.documentId == "doc123"
        assert ref.fileName == "test.pdf"
    
    def test_documentItemReference_to_string(self):
        """Test to_string() method"""
        ref = DocumentItemReference(documentId="doc123")
        assert ref.to_string() == "docItem:doc123"
        
        ref = DocumentItemReference(documentId="doc123", fileName="test.pdf")
        assert ref.to_string() == "docItem:doc123:test.pdf"


class TestDocumentReferenceList:
    """Test DocumentReferenceList model"""
    
    def test_documentReferenceList_creation(self):
        """Test creating DocumentReferenceList"""
        refList = DocumentReferenceList()
        assert len(refList.references) == 0
    
    def test_documentReferenceList_with_references(self):
        """Test DocumentReferenceList with references"""
        ref1 = DocumentListReference(label="task1_results")
        ref2 = DocumentItemReference(documentId="doc123")
        refList = DocumentReferenceList(references=[ref1, ref2])
        assert len(refList.references) == 2
    
    def test_documentReferenceList_to_string_list(self):
        """Test to_string_list() method"""
        ref1 = DocumentListReference(label="task1_results")
        ref2 = DocumentItemReference(documentId="doc123", fileName="test.pdf")
        refList = DocumentReferenceList(references=[ref1, ref2])
        stringList = refList.to_string_list()
        assert len(stringList) == 2
        assert "docList:task1_results" in stringList
        assert "docItem:doc123:test.pdf" in stringList
    
    def test_documentReferenceList_from_string_list_docList(self):
        """Test from_string_list() with docList references"""
        stringList = [
            "docList:task1_results",
            "docList:msg123:task2_results"
        ]
        refList = DocumentReferenceList.from_string_list(stringList)
        assert len(refList.references) == 2
        assert isinstance(refList.references[0], DocumentListReference)
        assert refList.references[0].label == "task1_results"
        assert refList.references[1].messageId == "msg123"
    
    def test_documentReferenceList_from_string_list_docItem(self):
        """Test from_string_list() with docItem references"""
        stringList = [
            "docItem:doc123",
            "docItem:doc456:test.pdf"
        ]
        refList = DocumentReferenceList.from_string_list(stringList)
        assert len(refList.references) == 2
        assert isinstance(refList.references[0], DocumentItemReference)
        assert refList.references[0].documentId == "doc123"
        assert refList.references[1].fileName == "test.pdf"
    
    def test_documentReferenceList_from_string_list_mixed(self):
        """Test from_string_list() with mixed reference types"""
        stringList = [
            "docList:task1_results",
            "docItem:doc123:test.pdf"
        ]
        refList = DocumentReferenceList.from_string_list(stringList)
        assert len(refList.references) == 2
        assert isinstance(refList.references[0], DocumentListReference)
        assert isinstance(refList.references[1], DocumentItemReference)
    
    def test_documentReferenceList_from_string_list_empty(self):
        """Test from_string_list() with empty list"""
        refList = DocumentReferenceList.from_string_list([])
        assert len(refList.references) == 0


if __name__ == "__main__":
    pytest.main([__file__, "-v"])

