# pyright: reportCallIssue=false

import os
from typing import Any, Final
from behave import given, when, then
from worklib.QueryData import kvs_query_data, kvs_query_data_place_id
from worklib import admin_data
from worklib.auth_as_employer import get_access_token
from worklib.findplaceinfo.find_place_data import fetch_place_members

expected_result = {
   "members": [
            {
              "id": "bb368ee9-c15f-40ef-acb0-c466df47d096",
              "parent_id": None,
              "user": {
                "username": "+79999956657"
              }
            }
          ]
}   




@when("get place info kvs")  # 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=kvs_query_data()["query"], variables=kvs_query_data_place_id()["variables"])
    context.place_info = data


@then("place info is valid")  # 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]["members"][0]["id"] == expected_result["members"][0]["id"]
    assert data["data"]["place"]["results"][0]["members"][0]["parent_id"] == expected_result["members"][0]["parent_id"]
    assert data["data"]["place"]["results"][0]["members"][0]["user"]["username"] == expected_result["members"][0]["user"]["username"]