"""library_storage_key: Mandantenpfade unter MEDIA_ROOT."""
from __future__ import annotations

import pytest

from media_storage import library_club_path_segment, library_media_kind_dir, library_storage_key

_HEX64 = "a" * 64


def test_library_media_kind_dir_mime_overrides_ext() -> None:
    assert library_media_kind_dir("image/png", ".bin") == "image"
    assert library_media_kind_dir("application/pdf", ".tmp") == "pdf"


def test_library_club_path_segment_slug_and_fallback() -> None:
    assert library_club_path_segment(3, "Grün & Weiß / Dojo!") == "gruen-weiss-dojo-c3"
    assert library_club_path_segment(3, None) == "verein-c3"
    assert library_club_path_segment(3, "   ") == "verein-c3"
    assert library_club_path_segment(12, "東道場") == "verein-c12"


def test_library_club_path_segment_long_name_truncated() -> None:
    long = "a" * 80
    seg = library_club_path_segment(5, long)
    assert seg == "a" * 48 + "-c5"


def test_library_storage_key_private_is_uploader_under_club() -> None:
    assert (
        library_storage_key(
            "private", 7, _HEX64, ".jpg", uploader_profile_id=99, club_name="Ost Dojo München"
        )
        == f"library/ost-dojo-muenchen-c7/image/{_HEX64}.u99.jpg"
    )


def test_library_storage_key_club_flat_under_club() -> None:
    assert (
        library_storage_key("club", 42, _HEX64, ".png", club_name="Demo-Verein")
        == f"library/demo-verein-c42/image/{_HEX64}.png"
    )


def test_library_storage_key_official() -> None:
    assert (
        library_storage_key("official", None, _HEX64, ".mp4", uploader_profile_id=1)
        == f"library/official/video/{_HEX64}.mp4"
    )


def test_library_storage_key_normalizes_visibility() -> None:
    assert (
        library_storage_key("  CLUB ", 1, _HEX64, "pdf", club_name="Alpha")
        == f"library/alpha-c1/pdf/{_HEX64}.pdf"
    )


def test_library_storage_key_other_bin() -> None:
    assert (
        library_storage_key("official", None, _HEX64, "", mime_type="application/octet-stream")
        == f"library/official/other/{_HEX64}.bin"
    )


def test_library_storage_key_private_requires_uploader() -> None:
    with pytest.raises(ValueError, match="uploader_profile_id"):
        library_storage_key("private", 1, _HEX64, ".jpg", club_name="V")


def test_library_storage_key_private_requires_club() -> None:
    with pytest.raises(ValueError, match="Verein"):
        library_storage_key("private", None, _HEX64, ".jpg", uploader_profile_id=1)


def test_library_storage_key_club_requires_id() -> None:
    with pytest.raises(ValueError, match="Verein"):
        library_storage_key("club", None, _HEX64, ".jpg", club_name="X")


def test_library_storage_key_club_id_positive() -> None:
    with pytest.raises(ValueError, match="positiv"):
        library_storage_key("club", 0, _HEX64, ".jpg", club_name="X")


def test_library_storage_key_invalid_visibility() -> None:
    with pytest.raises(ValueError, match="Sichtbarkeit"):
        library_storage_key("public", 1, _HEX64, ".jpg", club_name="X")


def test_library_storage_key_invalid_sha() -> None:
    with pytest.raises(ValueError, match="64"):
        library_storage_key("private", 1, "deadbeef", ".jpg", uploader_profile_id=1, club_name="X")


def test_library_storage_key_extension() -> None:
    with pytest.raises(ValueError):
        library_storage_key(
            "private", 1, _HEX64, "../x", uploader_profile_id=1, club_name="X"
        )


def test_library_storage_key_private_no_club_name_fallback() -> None:
    assert (
        library_storage_key("private", 2, _HEX64, ".jpg", uploader_profile_id=3)
        == f"library/verein-c2/image/{_HEX64}.u3.jpg"
    )
