"""Unit tests for backend/app/core/framing_scale.py."""

import pytest

from app.core.errors import AppError
from app.core.framing_scale import (
    FRAMING_CLOSE,
    FRAMING_INSERT,
    FRAMING_MEDIUM,
    FRAMING_WIDE,
    VALID_FRAMING_SCALES,
    get_framing_scale_or_raise,
)


def test_valid_framing_scales_frozenset_4_values():
    assert VALID_FRAMING_SCALES == frozenset({"close", "medium", "wide", "insert"})
    assert FRAMING_CLOSE == "close"
    assert FRAMING_MEDIUM == "medium"
    assert FRAMING_WIDE == "wide"
    assert FRAMING_INSERT == "insert"


def test_get_framing_scale_or_raise_returns_valid_enum():
    for value in ("close", "medium", "wide", "insert"):
        staging = {"framing_scale": value}
        assert get_framing_scale_or_raise(staging, where="test") == value


def test_get_framing_scale_or_raise_missing_staging_not_dict():
    with pytest.raises(AppError) as excinfo:
        get_framing_scale_or_raise(None, where="test.none")
    assert excinfo.value.code == "shot_staging.framing_scale_missing"
    assert "test.none" in excinfo.value.message
    assert excinfo.value.status_code == 422

    with pytest.raises(AppError) as excinfo:
        get_framing_scale_or_raise("not a dict", where="test.str")
    assert excinfo.value.code == "shot_staging.framing_scale_missing"


def test_get_framing_scale_or_raise_missing_key():
    with pytest.raises(AppError) as excinfo:
        get_framing_scale_or_raise({}, where="test.empty")
    assert excinfo.value.code == "shot_staging.framing_scale_missing"
    assert "test.empty" in excinfo.value.message
    assert "Legacy" in excinfo.value.message or "legacy" in excinfo.value.message


def test_get_framing_scale_or_raise_invalid_value():
    with pytest.raises(AppError) as excinfo:
        get_framing_scale_or_raise({"framing_scale": "huge"}, where="test.invalid")
    assert excinfo.value.code == "shot_staging.framing_scale_invalid"
    assert "test.invalid" in excinfo.value.message
    assert "huge" in excinfo.value.message
