import json
from pathlib import Path
from typing import Any, ClassVar

from tests.fixtures.agents import AgentFixtures
from tests.fixtures.agents.base import AgentProtocol
from verse_sdk.spec.spec_check import OtelGenAISpecCheck


class E2ERunner:
    _agent: AgentProtocol
    _specs: ClassVar[list[OtelGenAISpecCheck]] = []

    def __init__(self, name: str):
        self._agent = AgentFixtures().create(name)

    def assert_response_len(self, response: Any):
        """Assert the length of the response is something"""
        assert len(response) > 0

    def assert_validity(self):
        """Assert the validity of the specs."""
        for spec in self._specs:
            assert not spec.has_errors

    def run(self):
        """Run the test (e.g. sync or async)."""
        raise NotImplementedError("Subclasses must implement this method")

    def setup(self):
        """Load the spans from the export directory and create spec checks."""
        has_genai_spans = False
        for json_file in sorted(Path(".files").glob("*.json")):
            with open(json_file) as f:
                span_data = json.load(f)

                if not any(
                    # We only care to test GenAI generations
                    k.startswith("gen_ai.")
                    for k in span_data.get("attributes", {})
                ):
                    continue

                has_genai_spans = True
                spec = OtelGenAISpecCheck(span_data)
                self._specs.append(spec)

        if not has_genai_spans:
            raise ValueError("No GenAI spans found in the export directory")

    def teardown(self):
        """Reset the specs list."""
        self._specs = []
