# pyright: reportCallIssue=false

from __future__ import annotations

from behave import then, when

from KVSTest.testdata.kvs_test_data import KVSTestData

@when("query place members for created kvs place")  # pyright: ignore[reportGeneralTypeIssues]
def step_query_place_members(context) -> None:
    td = KVSTestData.from_behave_context(context)
    place_id = getattr(context, "kvs_place_id", None) or td.place_id
    assert isinstance(place_id, str) and place_id, "Нет kvs_place_id (place не создан)."
    context.kvs_place_members_response = td.query_place_members(place_id=place_id)

@then("kvs place members response has correct shape for created place")  # pyright: ignore[reportGeneralTypeIssues]
def step_assert_place_members_shape(context) -> None:
    td = KVSTestData.from_behave_context(context)
    place_id = getattr(context, "kvs_place_id", None) or td.place_id
    assert isinstance(place_id, str) and place_id, "Нет kvs_place_id."

    resp = getattr(context, "kvs_place_members_response", None)
    assert isinstance(resp, dict), f"Ответ GraphQL не dict: {resp!r}"
    assert "data" in resp, f"В ответе нет data: {resp!r}"
    place = resp.get("data", {}).get("place")
    assert isinstance(place, dict), f"data.place не объект: {place!r}"
    results = place.get("results")
    assert isinstance(results, list) and results, f"data.place.results пустой/не list: {results!r}"
    first = results[0]
    assert isinstance(first, dict), f"results[0] не объект: {first!r}"
    assert first.get("id") == place_id, f"Ожидали place.id={place_id!r}, получили: {first.get('id')!r}"
    members = first.get("members")
    assert isinstance(members, list), f"members не list: {members!r}"

