"""POST /api/exercises: keine Inline-Medien-Platzhalter beim ersten Anlegen (§11)."""

from __future__ import annotations

import os

import pytest
from fastapi.testclient import TestClient

os.environ.setdefault("SKIP_DB_MIGRATE", "1")

from auth import require_auth
from main import app
from tenant_context import TenantContext, get_tenant_context


@pytest.fixture
def client() -> TestClient:
    return TestClient(app)


@pytest.fixture(autouse=True)
def _clear_overrides() -> None:
    yield
    app.dependency_overrides.pop(require_auth, None)
    app.dependency_overrides.pop(get_tenant_context, None)


def test_post_exercise_rejects_inline_media_placeholder(client: TestClient) -> None:
    app.dependency_overrides[require_auth] = lambda: {"profile_id": 1, "role": "trainer"}
    app.dependency_overrides[get_tenant_context] = lambda: TenantContext(
        profile_id=1,
        global_role="trainer",
        effective_club_id=5,
        club_ids=frozenset({5}),
        memberships=[],
    )
    r = client.post(
        "/api/exercises",
        json={
            "title": "Mit Inline",
            "goal": "<p>Hallo {{exerciseMedia:7}}</p>",
            "execution": "<p>Schritt</p>",
            "visibility": "private",
            "status": "draft",
        },
        headers={"X-Auth-Token": "x"},
    )
    assert r.status_code == 400
    j = r.json()
    assert j.get("detail", {}).get("code") == "INLINE_EXERCISE_MEDIA_ON_CREATE"
