from __future__ import annotations

import typing

import pytest

from tests.responses import BaseResponseTest

if typing.TYPE_CHECKING:
    from _pytest.python import Function, Metafunc


def pytest_generate_tests(metafunc: Metafunc) -> None:
    cls_ = typing.cast("type[typing.Any] | None", metafunc.cls)

    if cls_ is not None and issubclass(cls_, BaseResponseTest):
        instance = cls_()
        if metafunc.definition.name not in instance._marks_table():
            return

        marker_name, args = instance._marks_table()[metafunc.definition.name]
        if marker_name != "parametrize":
            return  # other markers will be handled in `pytest_collection_modifyitems`
        metafunc.parametrize(*args)


def pytest_collection_modifyitems(items: list[Function]) -> None:
    for item in items:
        if isinstance(item.instance, BaseResponseTest):
            if item.obj.__name__ not in item.instance._marks_table():
                continue

            marker_name, args = item.instance._marks_table()[item.obj.__name__]
            if marker_name == "parametrize":
                continue
            item.add_marker(getattr(pytest.mark, marker_name)(*args))
