import pytest
from pydantic import ValidationError

from axm.catalog.entries import Identity, Owner
from axm.catalog.entries.data import Location
from axm_image.entries.image import ImageEntry, ImageSchema, ImageSpec


class TestImageEntry:
    """Tests for ImageEntry specialized subclass."""

    def test_image_entry_format(self) -> None:
        """ImageEntry has format='image'."""
        owner = Owner(team="vision", contact="v@v.com")
        identity = Identity(
            display_name="Camera Images",
            description="Test",
            domain="images",
            owner=owner,
        )
        entry = ImageEntry(
            name="test_images",
            version="1.0.0",
            identity=identity,
            data_schema=ImageSchema(spec=ImageSpec(encoding="jpg", channels=3)),
            location=Location(storage="filesystem", path_template="/data/img.jpg"),
        )
        assert entry.format == "image"

    def test_image_entry_schema_fields(self) -> None:
        """ImageEntry has typed schema with encoding, channels, etc."""
        owner = Owner(team="vision", contact="v@v.com")
        identity = Identity(
            display_name="Camera Images",
            description="Test",
            domain="images",
            owner=owner,
        )
        entry = ImageEntry(
            name="test_images",
            version="1.0.0",
            identity=identity,
            data_schema=ImageSchema(
                spec=ImageSpec(
                    encoding="jp2",
                    channels=3,
                    color_space="RGB",
                    width=1024,
                    height=1024,
                )
            ),
            location=Location(storage="filesystem", path_template="/data/img.jp2"),
        )
        assert entry.data_schema.spec.encoding == "jp2"
        assert entry.data_schema.spec.channels == 3
        assert entry.data_schema.spec.color_space == "RGB"
        assert entry.data_schema.spec.width == 1024
        assert entry.data_schema.spec.height == 1024

    def test_invalid_dimensions(self) -> None:
        """Should raise ValidationError for non-positive dimensions."""
        owner = Owner(team="vision", contact="v@v.com")
        identity = Identity(
            display_name="Bad Images",
            description="Test",
            domain="images",
            owner=owner,
        )
        with pytest.raises(ValidationError):
            ImageEntry(
                name="bad_images",
                version="1.0.0",
                identity=identity,
                data_schema=ImageSchema(
                    spec=ImageSpec(
                        encoding="jpg",
                        width=0,  # Invalid
                        height=-1,  # Invalid
                    )
                ),
                location=Location(storage="fs", path_template="/data/img.jpg"),
            )
