"""Tests for vector schema validation utilities."""

import json
from pathlib import Path

import jsonschema
import pytest

SCHEMA_PATH = (
    Path(__file__).parent.parent.parent / "vectors" / "schema" / "vector.schema.json"
)


@pytest.fixture
def schema():
    with open(SCHEMA_PATH) as f:
        return json.load(f)


def test_schema_is_valid_json_schema(schema):
    """The schema itself is a valid JSON Schema."""
    # jsonschema.validators.validator_for will return the right validator class
    validator_cls = jsonschema.validators.validator_for(schema)
    validator_cls.check_schema(schema)


def test_schema_requires_suite_field(schema):
    """Schema rejects objects without 'suite' field."""
    invalid = {"version": "1.0.0", "vectors": [{"id": "test-001", "description": "x", "inputs": {}, "expected": {}}]}
    with pytest.raises(jsonschema.ValidationError):
        jsonschema.validate(instance=invalid, schema=schema)


def test_schema_requires_vectors_field(schema):
    """Schema rejects objects without 'vectors' field."""
    invalid = {"suite": "test", "version": "1.0.0"}
    with pytest.raises(jsonschema.ValidationError):
        jsonschema.validate(instance=invalid, schema=schema)


def test_schema_requires_nonempty_vectors(schema):
    """Schema rejects empty vectors array."""
    invalid = {"suite": "test", "version": "1.0.0", "vectors": []}
    with pytest.raises(jsonschema.ValidationError):
        jsonschema.validate(instance=invalid, schema=schema)


def test_schema_requires_vector_id(schema):
    """Schema rejects vectors without 'id' field."""
    invalid = {
        "suite": "test",
        "version": "1.0.0",
        "vectors": [{"description": "x", "inputs": {}, "expected": {}}],
    }
    with pytest.raises(jsonschema.ValidationError):
        jsonschema.validate(instance=invalid, schema=schema)


def test_schema_validates_id_pattern(schema):
    """Schema rejects IDs that don't match the pattern."""
    invalid = {
        "suite": "test",
        "version": "1.0.0",
        "vectors": [{"id": "BAD_ID", "description": "x", "inputs": {}, "expected": {}}],
    }
    with pytest.raises(jsonschema.ValidationError):
        jsonschema.validate(instance=invalid, schema=schema)


def test_schema_accepts_valid_vector(schema):
    """Schema accepts a minimal valid vector."""
    valid = {
        "suite": "test",
        "version": "1.0.0",
        "vectors": [{"id": "test-001", "description": "A test", "inputs": {}, "expected": {}}],
    }
    jsonschema.validate(instance=valid, schema=schema)  # Should not raise
