import pytest

from libs.events import EventTopics
from libs.schemas.events import DocumentIngestedEventData, validate_event_data


@pytest.mark.integration
def test_doc_ingested_contract():
    """
    Contract test for DOC_INGESTED event.
    Verifies that the event data schema matches the expected Pydantic model.
    """
    # Sample valid payload data
    valid_data = {
        "doc_id": "doc_01H1V2W3X4Y5Z6",
        "filename": "test.pdf",
        "kind": "invoice",
        "source": "upload",
        "checksum_sha256": "a" * 64,
        "size_bytes": 1024,
        "mime_type": "application/pdf",
        "storage_path": "s3://bucket/key.pdf",
    }

    # 1. Verify it validates against the Pydantic model directly
    model = DocumentIngestedEventData(**valid_data)
    assert model.doc_id == valid_data["doc_id"]

    # 2. Verify it validates using the shared validation utility
    validated_model = validate_event_data(EventTopics.DOC_INGESTED, valid_data)
    assert isinstance(validated_model, DocumentIngestedEventData)
    assert validated_model.doc_id == valid_data["doc_id"]

    # 3. Verify invalid data fails
    invalid_data = valid_data.copy()
    del invalid_data["doc_id"]

    with pytest.raises(ValueError):
        validate_event_data(EventTopics.DOC_INGESTED, invalid_data)
