import pytest
from orchestrator.forms import FormValidationError
from orchestrator.types import SubscriptionLifecycle

from test.unit_tests.workflows import assert_complete, extract_state, run_workflow
from {{ product_types_module }}.{{ product.variable }} import {{ product.type }}


@pytest.mark.workflow
def test_happy_flow(responses, {{ product.variable }}_subscription):
    # given

    customer_id = "3f4fc287-0911-e511-80d0-005056956c1a"
    crm = CrmMocks(responses)
    crm.get_customer_by_uuid(customer_id)

    # TODO insert additional mocks, if needed (ImsMocks)

    # when

    init_state = {}

    result, process, step_log = run_workflow(
        "modify_{{ product.variable }}", [{"subscription_id": {{ product.variable }}_subscription}, init_state, {}]
    )

    # then

    assert_complete(result)
    state = extract_state(result)

    {{ product.variable }} = {{ product.type }}.from_subscription(state["subscription_id"])
    assert {{ product.variable }}.status == SubscriptionLifecycle.ACTIVE

{% for validation in validations if validation.field.modifiable in [defined, True] %}
@pytest.mark.workflow
def test_{{ validation.validation.id }}(responses, {{ product.variable }}_subscription):
    # given

    # TODO set test conditions or fixture so that "{{ validation.validation.description }}" triggers

    # when

    init_state = {}

    with pytest.raises(FormValidationError) as error:
        result, _, _ = run_workflow("modify_{{ product.variable }}", [{"subscription_id": {{ product.variable }}_subscription}, init_state, {}])

    # then

    assert error.value.errors[0]["msg"] == "{{ validation.validation.description }}"

{% endfor %}
