"""Pytest configuration for generated API tests.

This file is automatically @generated from the Meraki API specification.
Do not edit this file manually.

"""

import os
import re
from collections.abc import Generator
from contextlib import contextmanager

import pytest

from meraki_client.exceptions import InvalidRequestError, PermissionDeniedError, ResourceNotFoundError

# Skip all tests if API key is not set
pytestmark = pytest.mark.skipif(
    os.environ.get("MERAKI_DASHBOARD_API_KEY_TESTS") is None,
    reason="MERAKI_DASHBOARD_API_KEY_TESTS environment variable not set",
)

# Error messages that should skip the test rather than fail (exact match)
SKIP_ERROR_MESSAGES = [
    "This endpoint only supports MX devices",
    "This endpoint only supports MX networks",
    "This endpoint only supports secure routers",
    "This network does not support connectivity monitoring destinations",
    "Feature not supported",
    "This endpoint only supports Security Appliance networks",
    "Intrusion detection is not supported for this organization.",
    "Site-to-site VPN not enabled in your organization",
    "This endpoint only supports MV devices",
    "This endpoint only supports MG networks",
    "This endpoint only supports organizations with MX networks",
    "This endpoint only supports MS networks",
    "This network does not support NetFlow traffic reporting",
    "This organization does not support Dashboard branding",
    "This is not a Systems Manager network",
    "This endpoint only supports MS devices",
    "Network must have BLE advertising enabled in unique mode",
    "This endpoint only supports camera networks",
    "This endpoint only supports MG devices",
    "This device does not support SIM configurations.",
    "This endpoint only supports MX devices with wireless capabilities",
    "Single LAN settings are only available for networks with VLANs disabled",
    "Feature only supported for VPN participants",
    "This combined network does not contain a Systems Manager network",
    "Storm control is not supported on this network.",
    "OpenRoaming is not supported on any of the specified networks",
    "Radio settings are not supported on this device",
    "Static routes are unavailable for L2 switches",
    "This endpoint only supports MR devices",
    "This device does not support ESL.",
]

# Error message patterns that should skip the test (regex)
SKIP_ERROR_PATTERNS = [
    re.compile(r"Organization with ID \d+ does not support per-device licensing"),
    re.compile(r"This endpoint is only available for networks on MX \d+\.\d+ or above\."),
]


def _should_skip_error(message: str) -> bool:
    """Check if an error message should cause the test to skip."""
    if message in SKIP_ERROR_MESSAGES:
        return True
    return any(pattern.search(message) for pattern in SKIP_ERROR_PATTERNS)


@contextmanager
def skip_on_unsupported() -> Generator[None, None, None]:
    """Context manager that skips tests for unsupported features/devices."""
    try:
        yield
    except InvalidRequestError as err:
        if err.errors and _should_skip_error(err.errors[0]):
            pytest.skip(err.errors[0])
        raise
    except ResourceNotFoundError:
        pytest.skip("Resource not found")
    except PermissionDeniedError:
        pytest.skip("Permission denied")
