"""Basic tests for SAB-BENCH."""

import pytest
from pathlib import Path
import sys

# Add src to path
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))

from sab_bench.config import BenchmarkConfig, ModelConfig, DatasetConfig, EvaluatorConfig
from sab_bench.evaluators import get_evaluator
from sab_bench.datasets import get_dataset
from sab_bench.models import get_model


def test_config_loading():
    """Test configuration loading from YAML."""
    config_path = Path(__file__).parent.parent / "configs" / "basic.yaml"
    config = BenchmarkConfig.from_yaml(config_path)
    
    assert config.name == "basic_stub_test"
    assert config.evaluator.type == "stub"
    assert len(config.evaluator.models) == 2


def test_stub_model():
    """Test stub model produces consistent outputs."""
    config = ModelConfig(provider="stub", model="test-model")
    model = get_model(config)
    
    result1 = model.evaluate("test content")
    result2 = model.evaluate("test content")
    
    # Should be deterministic
    assert result1["decision"] == result2["decision"]
    assert result1["confidence"] == result2["confidence"]


def test_synthetic_dataset():
    """Test synthetic dataset generation."""
    config = DatasetConfig(
        type="synthetic",
        count=10,
        categories=["self_referential", "neutral"]
    )
    dataset = get_dataset(config)
    items = dataset.load()
    
    assert len(items) == 10
    assert all("id" in item for item in items)
    assert all("content" in item for item in items)
    assert all("category" in item for item in items)
    
    # Check categories
    categories = {item["category"] for item in items}
    assert "self_referential" in categories
    assert "neutral" in categories


def test_stub_evaluator():
    """Test stub evaluator produces valid results."""
    config = EvaluatorConfig(
        type="stub",
        models=[
            ModelConfig(provider="stub", model="model1"),
            ModelConfig(provider="stub", model="model2"),
        ]
    )
    evaluator = get_evaluator(config)
    
    item = {
        "id": "test_001",
        "content": "test content",
        "category": "neutral",
    }
    
    result = evaluator.evaluate(item)
    
    assert result["item_id"] == "test_001"
    assert "content_hash" in result
    assert "decisions" in result
    assert len(result["decisions"]) == 2
    assert "consensus" in result
    assert "agreement" in result
    assert 0.0 <= result["agreement"] <= 1.0


def test_multi_model_evaluator():
    """Test multi-model evaluator with stubs."""
    config = EvaluatorConfig(
        type="multi_model",
        models=[
            ModelConfig(provider="stub", model="model1", weight=1.0),
            ModelConfig(provider="stub", model="model2", weight=0.5),
        ]
    )
    evaluator = get_evaluator(config)
    
    item = {
        "id": "test_002",
        "content": "multi model test",
        "category": "self_referential",
    }
    
    result = evaluator.evaluate(item)
    
    assert "weighted_score" in result
    assert 0.0 <= result["weighted_score"] <= 1.0
    assert "agreement" in result


if __name__ == "__main__":
    pytest.main([__file__, "-v"])
