"""Tests for DIDL-Lite parsing and domain model mapping."""

from r36s_dlna_browser.dlna.models import (
    ItemType,
    MediaItem,
    classify_upnp_class,
    parse_didl_item,
)


class TestClassifyUpnpClass:
    def test_container(self):
        assert classify_upnp_class("object.container.storageFolder") == ItemType.CONTAINER

    def test_audio(self):
        assert classify_upnp_class("object.item.audioItem.musicTrack") == ItemType.AUDIO

    def test_video(self):
        assert classify_upnp_class("object.item.videoItem") == ItemType.VIDEO

    def test_image(self):
        assert classify_upnp_class("object.item.imageItem.photo") == ItemType.IMAGE

    def test_unknown(self):
        assert classify_upnp_class("object.item.something") == ItemType.UNKNOWN

    def test_empty(self):
        assert classify_upnp_class("") == ItemType.UNKNOWN


class TestParseDIDLItem:
    def test_audio_item(self):
        didl = {
            "id": "42",
            "title": "My Song",
            "parent_id": "10",
            "upnp_class": "object.item.audioItem.musicTrack",
            "resources": [
                {
                    "url": "http://server/song.mp3",
                    "protocol_info": "http-get:*:audio/mpeg:*",
                    "size": "5000000",
                    "duration": "0:03:45",
                }
            ],
            "album_art_uri": "http://server/art.jpg",
        }
        item = parse_didl_item(didl)
        assert item.object_id == "42"
        assert item.title == "My Song"
        assert item.item_type == ItemType.AUDIO
        assert item.resource_url == "http://server/song.mp3"
        assert item.mime_type == "http-get:*:audio/mpeg:*"
        assert item.size == 5000000
        assert item.duration == "0:03:45"
        assert item.album_art_url == "http://server/art.jpg"
        assert not item.is_container

    def test_container_item(self):
        didl = {
            "id": "5",
            "title": "Music",
            "parent_id": "0",
            "upnp_class": "object.container.storageFolder",
            "child_count": "12",
        }
        item = parse_didl_item(didl)
        assert item.object_id == "5"
        assert item.title == "Music"
        assert item.item_type == ItemType.CONTAINER
        assert item.child_count == 12
        assert item.is_container

    def test_missing_resources(self):
        didl = {"id": "99", "title": "NoRes", "upnp_class": "object.item.audioItem"}
        item = parse_didl_item(didl)
        assert item.resource_url == ""
        assert item.size == 0

    def test_single_resource_dict(self):
        didl = {
            "id": "1",
            "title": "Track",
            "upnp_class": "object.item.audioItem",
            "resources": {"url": "http://srv/track.flac", "size": "100"},
        }
        item = parse_didl_item(didl)
        assert item.resource_url == "http://srv/track.flac"
        assert item.size == 100

    def test_resource_string(self):
        didl = {
            "id": "2",
            "title": "Track2",
            "upnp_class": "object.item.videoItem",
            "resources": ["http://srv/vid.mp4"],
        }
        item = parse_didl_item(didl)
        assert item.resource_url == "http://srv/vid.mp4"

    def test_prefers_media_resource_over_subtitle_resource(self):
        didl = {
            "id": "3",
            "title": "Movie",
            "upnp_class": "object.item.videoItem",
            "resources": [
                {
                    "url": "http://srv/movie.srt",
                    "protocol_info": "http-get:*:text/srt:*",
                },
                {
                    "url": "http://srv/movie.mkv",
                    "protocol_info": "http-get:*:video/x-matroska:*",
                    "size": "1200",
                },
            ],
        }
        item = parse_didl_item(didl)
        assert item.resource_url == "http://srv/movie.mkv"
        assert item.mime_type == "http-get:*:video/x-matroska:*"

    def test_alt_key_names(self):
        """Handles alternative key formats (@id, @parentID, class, etc.)."""
        didl = {
            "@id": "7",
            "title": "Alt",
            "@parentID": "3",
            "class": "object.container",
            "@childCount": "5",
        }
        item = parse_didl_item(didl)
        assert item.object_id == "7"
        assert item.parent_id == "3"
        assert item.item_type == ItemType.CONTAINER
        assert item.child_count == 5

    def test_empty_dict(self):
        item = parse_didl_item({})
        assert item.object_id == ""
        assert item.title == ""
        assert item.item_type == ItemType.UNKNOWN
