"""Unit tests for path_utils.py — path resolution logic."""

from media_library_viewer_api.path_utils import (
    apply_remote_path_prefix,
    map_path_to_media_root,
    resolve_remote_media_path,
)


class TestApplyRemotePathPrefix:
    def test_empty_path(self):
        assert apply_remote_path_prefix("", "/srv") == ""

    def test_empty_prefix(self):
        assert apply_remote_path_prefix("/media/shows", "") == "/media/shows"

    def test_whitespace_prefix(self):
        assert apply_remote_path_prefix("/media/shows", "   ") == "/media/shows"

    def test_basic_prefix(self):
        assert apply_remote_path_prefix("/media/shows/Breaking Bad", "/srv") == "/srv/media/shows/Breaking Bad"

    def test_already_prefixed(self):
        assert apply_remote_path_prefix("/srv/media/shows", "/srv") == "/srv/media/shows"

    def test_relative_path(self):
        result = apply_remote_path_prefix("media/shows", "/srv")
        assert result == "/srv/media/shows"

    def test_trailing_slash_prefix(self):
        assert apply_remote_path_prefix("/media/file.mkv", "/srv/") == "/srv/media/file.mkv"

    def test_path_with_spaces(self):
        assert (
            apply_remote_path_prefix("/media/My Movie (2024)/file.mkv", "/srv") == "/srv/media/My Movie (2024)/file.mkv"
        )


class TestMapPathToMediaRoot:
    def test_empty_path(self):
        assert map_path_to_media_root("", "/srv/media") == ""

    def test_empty_root(self):
        assert map_path_to_media_root("/media/shows", "") == "/media/shows"

    def test_already_under_root(self):
        result = map_path_to_media_root("/srv/media/shows/Breaking Bad", "/srv/media")
        assert result == "/srv/media/shows/Breaking Bad"

    def test_anchor_mapping(self):
        # Jellyfin path /media/shows/X -> media_root /srv/media -> /srv/media/shows/X
        result = map_path_to_media_root("/media/shows/Breaking Bad", "/srv/media")
        assert result == "/srv/media/shows/Breaking Bad"

    def test_anchor_mapping_deeper(self):
        result = map_path_to_media_root("/media/movies/Movie (2024)/file.mkv", "/srv/media")
        assert result == "/srv/media/movies/Movie (2024)/file.mkv"

    def test_no_anchor_match(self):
        # Root basename 'data' does not appear in path
        result = map_path_to_media_root("/media/shows/X", "/srv/data")
        assert result == "/media/shows/X"

    def test_root_is_just_anchor(self):
        result = map_path_to_media_root("/media/shows/file.mkv", "/media")
        assert result == "/media/shows/file.mkv"

    def test_path_equals_root(self):
        result = map_path_to_media_root("/srv/media", "/srv/media")
        assert result == "/srv/media"


class TestResolveRemoteMediaPath:
    def test_empty_path(self):
        assert resolve_remote_media_path("", "/srv/media", "/srv") == ""

    def test_mapping_takes_priority(self):
        # media_root mapping should work
        result = resolve_remote_media_path("/media/shows/X", "/srv/media", "/fallback")
        assert result == "/srv/media/shows/X"

    def test_fallback_prefix_when_no_anchor(self):
        # No anchor match -> fallback prefix applied
        result = resolve_remote_media_path("/stuff/file.mkv", "/srv/media", "/fallback")
        assert result == "/fallback/stuff/file.mkv"

    def test_no_mapping_no_prefix(self):
        result = resolve_remote_media_path("/stuff/file.mkv", "", "")
        assert result == "/stuff/file.mkv"

    def test_already_resolved(self):
        result = resolve_remote_media_path("/srv/media/file.mkv", "/srv/media", "/srv")
        assert result == "/srv/media/file.mkv"
