# pyright: reportCallIssue=false

from __future__ import annotations

from behave import then, when

from KVSTest.testdata.kvs_test_data import KVSTestData


@when("create two users for kvs")  # pyright: ignore[reportGeneralTypeIssues]
def step_create_two_users_for_kvs(context) -> None:
    td = KVSTestData.from_behave_context(context)
    uid1, _ = td.create_new_user()
    uid2, _ = td.create_new_user()
    context.kvs_user_ids = [uid1, uid2]


@when("add both users to kvs place")  # pyright: ignore[reportGeneralTypeIssues]
def step_add_both_users_to_place(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 не создан)."

    user_ids = getattr(context, "kvs_user_ids", None)
    assert isinstance(user_ids, list) and len(user_ids) == 2, "Нет kvs_user_ids (ожидали двух созданных пользователей)."
    assert all(isinstance(x, str) and x for x in user_ids), f"Некорректные user_ids: {user_ids!r}"

    context.kvs_add_user_to_place_responses = [
        td.add_user_to_place(account_id=user_ids[0], place_id=place_id),
        td.add_user_to_place(account_id=user_ids[1], place_id=place_id),
    ]



@when("query members by created kvs place")  # pyright: ignore[reportGeneralTypeIssues]
def step_query_members_by_place(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."
    context.kvs_members_response = td.query_members_by_place(place_id=place_id)


@then("members response contains two created users with statuses accepted and pending")  # pyright: ignore[reportGeneralTypeIssues]
def step_assert_members_initial_statuses(context) -> None:
    user_ids = getattr(context, "kvs_user_ids", None)
    assert isinstance(user_ids, list) and len(user_ids) == 2, "Нет kvs_user_ids."
    uid1, uid2 = user_ids

    resp = getattr(context, "kvs_members_response", None)
    assert isinstance(resp, dict) and "data" in resp, f"Некорректный members ответ: {resp!r}"
    results = resp.get("data", {}).get("members", {}).get("results")
    assert isinstance(results, list), f"data.members.results не list: {results!r}"

    by_id = {r.get("id"): r for r in results if isinstance(r, dict)}
    assert uid1 in by_id and uid2 in by_id, f"id не совпали с созданными пользователями. ожидали={user_ids!r} получили={list(by_id.keys())!r}"
    assert by_id[uid1].get("status") == "accepted", f"Первый пользователь должен быть accepted: {by_id.get(uid1)!r}"
    assert by_id[uid2].get("status") == "pending", f"Второй пользователь должен быть pending: {by_id.get(uid2)!r}"


@when("update second kvs user status to accepted")  # pyright: ignore[reportGeneralTypeIssues]
def step_update_second_user_status(context) -> None:
    td = KVSTestData.from_behave_context(context)
    place_id = getattr(context, "kvs_place_id", None) or td.place_id
    user_ids = getattr(context, "kvs_user_ids", None)
    assert isinstance(place_id, str) and place_id, "Нет kvs_place_id."
    assert isinstance(user_ids, list) and len(user_ids) == 2, "Нет kvs_user_ids."
    context.kvs_update_status_response_2 = td.update_member_status(place_id=place_id, user_id=user_ids[1], status="accepted")


@then("members response contains two created users with status accepted")  # pyright: ignore[reportGeneralTypeIssues]
def step_assert_members_final_statuses(context) -> None:
    user_ids = getattr(context, "kvs_user_ids", None)
    assert isinstance(user_ids, list) and len(user_ids) == 2, "Нет kvs_user_ids."
    uid1, uid2 = user_ids

    resp = getattr(context, "kvs_members_response", None)
    assert isinstance(resp, dict) and "data" in resp, f"Некорректный members ответ: {resp!r}"
    results = resp.get("data", {}).get("members", {}).get("results")
    assert isinstance(results, list), f"data.members.results не list: {results!r}"

    by_id = {r.get("id"): r for r in results if isinstance(r, dict)}
    assert uid1 in by_id and uid2 in by_id, f"id не совпали с созданными пользователями. ожидали={user_ids!r} получили={list(by_id.keys())!r}"
    assert by_id[uid1].get("status") == "accepted", f"Первый пользователь должен быть accepted: {by_id.get(uid1)!r}"
    assert by_id[uid2].get("status") == "accepted", f"Второй пользователь должен быть accepted: {by_id.get(uid2)!r}"

