import json

from iisi_app_core.rest import conflict, feature_disabled, unprocessable_entity


def _json_body(response):  # noqa: ANN001, ANN202
    return json.loads(response["body"])


def test_unprocessable_entity_returns_422_with_validation_error_code() -> None:
    response = unprocessable_entity("Invalid input", details=[{"field": "email"}])
    body = _json_body(response)

    assert response["statusCode"] == 422
    assert response["headers"]["Content-Type"] == "application/json"
    assert body["code"] == "VALIDATION_ERROR"
    assert body["message"] == "Invalid input"
    assert body["details"] == [{"field": "email"}]


def test_conflict_returns_409_with_custom_error_code() -> None:
    response = conflict("User already exists", code="USER_ALREADY_EXISTS", details=[{"field": "email"}])
    body = _json_body(response)

    assert response["statusCode"] == 409
    assert response["headers"]["Content-Type"] == "application/json"
    assert body["code"] == "USER_ALREADY_EXISTS"
    assert body["message"] == "User already exists"
    assert body["details"] == [{"field": "email"}]


def test_feature_disabled_uses_spec_error_code_by_default() -> None:
    response = feature_disabled()
    body = _json_body(response)

    assert response["statusCode"] == 403
    assert body["code"] == "FEATURE_DISABLED"
    assert body["message"] == "Feature disabled"
