"""Integration tests for the full XYGLUE engine pipeline."""

import pytest
from xyglue import Engine, RunResult, EngineConfig, Brief
from xyglue.config import EngineConfig


class TestEngine:
    @pytest.fixture
    def engine(self):
        return Engine()

    async def test_basic_run(self, engine):
        result = await engine.run("Build a simple test", mode="auto")
        assert isinstance(result, RunResult)
        assert result.brief is not None

    async def test_brief_format(self, engine):
        result = await engine.run("Test goal", mode="auto")
        formatted = result.brief.format()
        assert isinstance(formatted, str)
        assert len(formatted) > 0

    async def test_auto_mode(self, engine):
        result = await engine.run("Simple task", mode="auto")
        assert result.brief is not None

    async def test_semi_mode(self, engine):
        result = await engine.run("Simple task", mode="semi")
        assert result.brief is not None

    async def test_custom_divergence(self):
        engine = Engine(divergence="high")
        result = await engine.run("Complex distributed system", mode="auto")
        assert result.brief is not None

    async def test_no_api_key_works(self, engine):
        result = await engine.run("Build an API", mode="auto")
        assert result.brief is not None
        assert len(result.brief.format()) > 0


class TestEngineConfig:
    def test_default_config(self):
        cfg = EngineConfig()
        assert cfg.mode == "semi"
        assert cfg.divergence == "medium"

    def test_custom_config(self):
        cfg = EngineConfig(mode="auto", divergence="high")
        assert cfg.mode == "auto"
        assert cfg.divergence == "high"

    def test_validate_good_config(self):
        cfg = EngineConfig()
        issues = cfg.validate()
        assert issues == []

    def test_validate_bad_threshold(self):
        cfg = EngineConfig(template_quality_threshold=1.5)
        issues = cfg.validate()
        assert len(issues) > 0


class TestRunResult:
    async def test_run_result_fields(self):
        engine = Engine()
        result = await engine.run("test", mode="auto")
        assert hasattr(result, "brief")


class TestBrief:
    async def test_brief_creation(self):
        engine = Engine()
        result = await engine.run("Build an API", mode="auto")
        brief = result.brief
        assert isinstance(brief, Brief)
        assert isinstance(brief.format(), str)
