# #region TestAgentChat.ApiFixtures [C:2] [TYPE Module] [SEMANTICS test,agent,fixtures,api]
# @BRIEF Materialize API fixtures from JSON — verify fixture structure matches expected API contracts.
# @RELATION BINDS_TO -> [AgentChat.Api.Conversations]
import json
from pathlib import Path

import pytest

FIXTURES_DIR = Path(__file__).resolve().parent.parent.parent.parent / "specs" / "033-gradio-agent-chat" / "fixtures" / "api"


# #region TestAgentChat.ApiFixtures.Load [C:2] [TYPE Function] [SEMANTICS test,fixture,load]
# @BRIEF All 9 API fixture files load without parse errors.
def test_all_api_fixtures_load():
    """Verify all API fixture files exist and load as valid JSON."""
    expected_fixtures = [
        "conversations_list_valid.json",
        "conversations_list_empty.json",
        "conversations_list_missing_auth.json",
        "conversations_list_invalid_page.json",
        "conversations_list_external_fail.json",
        "history_valid.json",
        "history_not_found.json",
        "service_token_valid.json",
        "service_token_invalid_secret.json",
    ]
    for name in expected_fixtures:
        path = FIXTURES_DIR / name
        assert path.exists(), f"Fixture not found: {name}"
        with open(path) as f:
            data = json.load(f)
        assert "fixture_id" in data, f"{name}: missing fixture_id"
        assert "verifies" in data, f"{name}: missing verifies"
        assert "input" in data, f"{name}: missing input"
        assert "expected" in data, f"{name}: missing expected"
# #endregion TestAgentChat.ApiFixtures.Load


# #region TestAgentChat.ApiFixtures.ConversationsList [C:2] [TYPE Function] [SEMANTICS test,fixture,conversations]
# @BRIEF Conversations list fixtures have correct structure: valid returns 200+items, empty returns 200+[].
def test_conversations_list_valid():
    """FX_AgentChat.Conversations.ListValid → 200 with items array."""
    with open(FIXTURES_DIR / "conversations_list_valid.json") as f:
        fixture = json.load(f)
    assert fixture["fixture_id"] == "FX_AgentChat.Conversations.ListValid"
    assert fixture["expected"]["status"] == 200
    assert "items" in fixture["expected"]["body"]
    assert fixture["input"]["method"] == "GET"


def test_conversations_list_empty():
    """FX_AgentChat.Conversations.ListEmpty → 200 with empty items."""
    with open(FIXTURES_DIR / "conversations_list_empty.json") as f:
        fixture = json.load(f)
    assert fixture["fixture_id"] == "FX_AgentChat.Conversations.ListEmpty"
    assert fixture["expected"]["status"] == 200
    assert fixture["expected"]["body"]["items"] == []


def test_conversations_list_missing_auth():
    """FX_AgentChat.Conversations.ListMissingAuth → 401."""
    with open(FIXTURES_DIR / "conversations_list_missing_auth.json") as f:
        fixture = json.load(f)
    assert fixture["fixture_id"] == "FX_AgentChat.Conversations.ListMissingAuth"
    assert fixture["expected"]["status"] == 401
# #endregion TestAgentChat.ApiFixtures.ConversationsList


# #region TestAgentChat.ApiFixtures.History [C:2] [TYPE Function] [SEMANTICS test,fixture,history]
# @BRIEF History fixtures have correct structure.
def test_history_valid():
    """FX_AgentChat.History.Valid → 200 with messages."""
    with open(FIXTURES_DIR / "history_valid.json") as f:
        fixture = json.load(f)
    assert fixture["fixture_id"] == "FX_AgentChat.History.Valid"
    assert fixture["expected"]["status"] == 200
    assert "items" in fixture["expected"]["body"]


def test_history_not_found():
    """FX_AgentChat.History.NotFound → 404."""
    with open(FIXTURES_DIR / "history_not_found.json") as f:
        fixture = json.load(f)
    assert fixture["fixture_id"] == "FX_AgentChat.History.NotFound"
    assert fixture["expected"]["status"] == 404
# #endregion TestAgentChat.ApiFixtures.History


# #region TestAgentChat.ApiFixtures.ServiceToken [C:2] [TYPE Function] [SEMANTICS test,fixture,service-token]
# @BRIEF Service token fixtures.
def test_service_token_valid():
    """FX_AgentChat.ServiceToken.Valid → 200 with token."""
    with open(FIXTURES_DIR / "service_token_valid.json") as f:
        fixture = json.load(f)
    assert fixture["fixture_id"] == "FX_AgentChat.ServiceToken.Valid"
    assert fixture["expected"]["status"] == 200


def test_service_token_invalid():
    """FX_AgentChat.ServiceToken.InvalidSecret → 401."""
    with open(FIXTURES_DIR / "service_token_invalid_secret.json") as f:
        fixture = json.load(f)
    assert fixture["fixture_id"] == "FX_AgentChat.ServiceToken.InvalidSecret"
    assert fixture["expected"]["status"] == 401
# #endregion TestAgentChat.ApiFixtures.ServiceToken
# #endregion TestAgentChat.ApiFixtures
