from behave import given, when, then
from typing import Final
from worklib import admin_data
from worklib.findplaceinfo.find_place_data import fetch_place_members
from worklib.QueryData import query_data, query_data_place_id_variables
# pyright: ignore[reportGeneralTypeIssues]

_EXPECTED_RESULT: Final[dict[str, str]] = {
    "id": "682b071a163ac2a0995355be",
    "place_type": "street",
    "name": "ул. Мебельная",
}
@when("get place info")  # pyright: ignore[reportGeneralTypeIssues]
def step_get_place_info(context):
    token = getattr(context, "access_token", None) or admin_data.get_or_create_user("tester").access_token
    data = fetch_place_members(access_token=token, query=query_data()["query"], variables=query_data_place_id_variables()["variables"])
    context.place_info = data

@then("place info is valid for query data")  # pyright: ignore[reportGeneralTypeIssues]
def step_place_info_valid(context):
    data = getattr(context, "place_info", None)
    assert isinstance(data, dict), "Ответ GraphQL не dict"
    assert "data" in data or "place" in str(data), f"Не похоже на успешный GraphQL ответ: {data}"
    assert data["data"]["place"]["results"][0]["id"] == _EXPECTED_RESULT["id"]
    assert data["data"]["place"]["results"][0]["place_type"] == _EXPECTED_RESULT["place_type"]
    assert data["data"]["place"]["results"][0]["name"] == _EXPECTED_RESULT["name"]
