"""Vereins-Übung: Copyright-Pflicht für angehängte Archiv-Dateien."""
from __future__ import annotations

import pytest
from fastapi import HTTPException

import routers.exercises as exercises_mod
from routers.exercises import apply_club_exercise_media_copyright_rules


def test_apply_club_exercise_media_copyright_skips_non_club() -> None:
    apply_club_exercise_media_copyright_rules(object(), 1, "private")


def test_apply_club_exercise_media_copyright_missing_copyright() -> None:
    orig = exercises_mod._fetch_exercise_linked_file_assets

    def mock_fetch(_cur, eid: int):
        assert eid == 42
        return [
            {
                "id": 10,
                "visibility": "private",
                "club_id": 1,
                "lifecycle_state": "active",
                "copyright_notice": "",
                "original_filename": "x.jpg",
            }
        ]

    exercises_mod._fetch_exercise_linked_file_assets = mock_fetch
    try:
        with pytest.raises(HTTPException) as ei:
            apply_club_exercise_media_copyright_rules(object(), 42, "club")
        assert ei.value.status_code == 422
        d = ei.value.detail
        assert isinstance(d, dict)
        assert d.get("code") == "CLUB_MEDIA_COPYRIGHT_REQUIRED"
    finally:
        exercises_mod._fetch_exercise_linked_file_assets = orig


def test_apply_club_exercise_media_copyright_ok() -> None:
    orig = exercises_mod._fetch_exercise_linked_file_assets

    def mock_fetch(_cur, _eid: int):
        return [
            {
                "id": 10,
                "visibility": "private",
                "club_id": 1,
                "lifecycle_state": "active",
                "copyright_notice": "© Verein 2026",
                "original_filename": "x.jpg",
            }
        ]

    exercises_mod._fetch_exercise_linked_file_assets = mock_fetch
    try:
        apply_club_exercise_media_copyright_rules(object(), 42, "club")
    finally:
        exercises_mod._fetch_exercise_linked_file_assets = orig


def test_apply_club_exercise_media_copyright_applies_default() -> None:
    """Nach Bestätigung setzt der Client default_club_media_copyright — Backend schreibt auf Assets."""
    orig_fetch = exercises_mod._fetch_exercise_linked_file_assets
    calls = {"n": 0}

    def mock_fetch(_cur, _eid: int):
        calls["n"] += 1
        if calls["n"] == 1:
            return [
                {
                    "id": 10,
                    "visibility": "private",
                    "club_id": 1,
                    "lifecycle_state": "active",
                    "copyright_notice": "",
                    "original_filename": "x.jpg",
                }
            ]
        return [
            {
                "id": 10,
                "visibility": "private",
                "club_id": 1,
                "lifecycle_state": "active",
                "copyright_notice": "© Testverein",
                "original_filename": "x.jpg",
            }
        ]

    class MockCur:
        def execute(self, *args, **kwargs):
            pass

    exercises_mod._fetch_exercise_linked_file_assets = mock_fetch
    try:
        apply_club_exercise_media_copyright_rules(
            MockCur(),
            42,
            "club",
            default_club_media_copyright="© Testverein",
        )
        assert calls["n"] == 2
    finally:
        exercises_mod._fetch_exercise_linked_file_assets = orig_fetch
