"""
Integration test against a live LM Studio instance.
Skipped automatically if LM Studio is not reachable.
"""
import pytest
import httpx


def lmstudio_available() -> bool:
    try:
        r = httpx.get("http://host.docker.internal:1234/v1/models", timeout=3)
        return r.status_code == 200
    except Exception:
        return False


@pytest.mark.skipif(not lmstudio_available(), reason="LM Studio not reachable at host.docker.internal:1234")
@pytest.mark.asyncio
async def test_lmstudio_health_check():
    from ai.lmstudio_provider import LMStudioProvider
    provider = LMStudioProvider(
        base_url="http://host.docker.internal:1234",
        model="gemma-4-e4b-it",
    )
    ok = await provider.health_check()
    assert ok, "LM Studio health check failed"


@pytest.mark.skipif(not lmstudio_available(), reason="LM Studio not reachable at host.docker.internal:1234")
@pytest.mark.asyncio
async def test_lmstudio_classify():
    from ai.lmstudio_provider import LMStudioProvider
    from services.classifier import _DEFAULT_SYSTEM_PROMPT

    provider = LMStudioProvider(
        base_url="http://host.docker.internal:1234",
        model="gemma-4-e4b-it",
    )
    result = await provider.classify(
        document_text="This document is an invoice for software development services.",
        existing_topics=["Finance", "Legal", "HR"],
        system_prompt=_DEFAULT_SYSTEM_PROMPT,
    )
    # Result should have some topics assigned or suggested
    assert isinstance(result.topics, list)
    assert isinstance(result.suggested_new_topics, list)
