# pyright: reportCallIssue=false

from __future__ import annotations

from behave import then, when

from Pass_request.testdata.pass_request_test_data import PassRequestTestData


@when("prepare nested places and employees for pass request approval flow")  # pyright: ignore[reportGeneralTypeIssues]
def step_prepare_nested_places_and_employees(context) -> None:
    td = PassRequestTestData.from_behave_context(context)
    td.prepare_pass_request_approval_flow()
    context.place1_id = td.place1_id
    context.place2_id = td.place2_id
    context.place3_id = td.place3_id
    context.new_employee_token = td.new_access_token


@when("create pass in place #3 for approval flow")  # pyright: ignore[reportGeneralTypeIssues]
def step_create_pass_in_place3(context) -> None:
    td = PassRequestTestData.from_behave_context(context)
    pass_id = td.create_pass()
    context.pass_id = pass_id


@when("query passRequests by created pass_id with my token")  # pyright: ignore[reportGeneralTypeIssues]
def step_query_pass_requests_my_token(context) -> None:
    td = PassRequestTestData.from_behave_context(context)
    token = td.ensure_token()
    resp = td.wait_for_pass_request(token=token, pass_id=getattr(context, "pass_id", None))
    context.pass_requests_my = resp
    pr = td.extract_single_pass_request(resp)
    context.pass_request_id = pr["id"]
    context.pass_request_status = pr.get("status")
    expected_place_id = td.expected_pass_request_place_id
    assert isinstance(expected_place_id, str) and expected_place_id, "Нет expected_pass_request_place_id в testdata."
    assert pr.get("place_id") == expected_place_id, (
        "PassRequest должен создаваться в родительском месте от места, где создан pass. "
        f"Ожидали place_id={expected_place_id!r}, получили: {pr.get('place_id')!r}"
    )


@then("pass request status is pending")  # pyright: ignore[reportGeneralTypeIssues]
def step_assert_pending(context) -> None:
    status = getattr(context, "pass_request_status", None)
    assert status == "pending", f"Ожидали status=pending, получили: {status!r}"


@when("approve pass request with my token")  # pyright: ignore[reportGeneralTypeIssues]
def step_approve_with_my_token(context) -> None:
    td = PassRequestTestData.from_behave_context(context)
    token = td.ensure_token()
    pr_id = getattr(context, "pass_request_id", None)
    assert isinstance(pr_id, str) and pr_id, "Нет pass_request_id для approve."
    td.approve_pass_request(pass_request_id=pr_id, token=token)


@when("re-query passRequests by created pass_id with my token")  # pyright: ignore[reportGeneralTypeIssues]
def step_requery_my_token(context) -> None:
    td = PassRequestTestData.from_behave_context(context)
    token = td.ensure_token()
    resp = td.wait_for_pass_request(token=token, pass_id=getattr(context, "pass_id", None))
    context.pass_requests_my_after = resp
    pr = td.extract_single_pass_request(resp)
    context.pass_request_status = pr.get("status")
    expected_place_id = td.expected_pass_request_place_id
    assert isinstance(expected_place_id, str) and expected_place_id, "Нет expected_pass_request_place_id в testdata."
    assert pr.get("place_id") == expected_place_id, (
        "PassRequest должен оставаться в родительском месте от места, где создан pass. "
        f"Ожидали place_id={expected_place_id!r}, получили: {pr.get('place_id')!r}"
    )


@when("approve pass request with new employee token")  # pyright: ignore[reportGeneralTypeIssues]
def step_approve_with_new_employee_token(context) -> None:
    td = PassRequestTestData.from_behave_context(context)
    pr_id = getattr(context, "pass_request_id", None)
    assert isinstance(pr_id, str) and pr_id, "Нет pass_request_id для approve."
    token = getattr(context, "new_employee_token", None)
    assert isinstance(token, str) and token.strip(), "Нет new_employee_token."
    td.approve_pass_request(pass_request_id=pr_id, token=token)


@when("query passRequests by created pass_id with new employee token")  # pyright: ignore[reportGeneralTypeIssues]
def step_query_with_new_employee_token(context) -> None:
    td = PassRequestTestData.from_behave_context(context)
    token = getattr(context, "new_employee_token", None)
    assert isinstance(token, str) and token.strip(), "Нет new_employee_token."
    resp = td.wait_for_pass_request(token=token, pass_id=getattr(context, "pass_id", None))
    context.pass_requests_new = resp
    pr = td.extract_single_pass_request(resp)
    context.pass_request_status = pr.get("status")
    expected_place_id = td.expected_pass_request_place_id
    assert isinstance(expected_place_id, str) and expected_place_id, "Нет expected_pass_request_place_id в testdata."
    assert pr.get("place_id") == expected_place_id, (
        "PassRequest должен создаваться в родительском месте от места, где создан pass. "
        f"Ожидали place_id={expected_place_id!r}, получили: {pr.get('place_id')!r}"
    )


@then("pass request status is active")  # pyright: ignore[reportGeneralTypeIssues]
def step_assert_active(context) -> None:
    status = getattr(context, "pass_request_status", None)
    assert status == "active", f"Ожидали status=active, получили: {status!r}"


@when("reject pass request with my token")  # pyright: ignore[reportGeneralTypeIssues]
def step_reject_with_my_token(context) -> None:
    td = PassRequestTestData.from_behave_context(context)
    token = td.ensure_token()
    pr_id = getattr(context, "pass_request_id", None)
    assert isinstance(pr_id, str) and pr_id, "Нет pass_request_id для reject."
    td.reject_pass_request(pass_request_id=pr_id, token=token)


@then("pass request status is not active")  # pyright: ignore[reportGeneralTypeIssues]
def step_assert_not_active(context) -> None:
    status = getattr(context, "pass_request_status", None)
    assert status != "active", f"После reject статус не должен стать active. Получили: {status!r}"

