{# Terminate workflow -#}

{%-if validations %}
from uuid import UUID
from pydantic import root_validator
{% endif %}
from orchestrator.forms import FormPage
from orchestrator.forms.validators import DisplaySubscription
from orchestrator.types import InputForm, State, UUIDstr
from orchestrator.workflow import StepList, begin, step
from orchestrator.workflows.utils import terminate_workflow

from {{ product_types_module }}.{{ product.variable }} import {{ product.type }}
{% include 'additional_terminate_imports.j2' ignore missing %}

@step("Load initial state")
def load_initial_state(subscription: {{ product.type }}) -> State:
    {% if product.nso_service_id_path %}
    nso_service_id = str(subscription.{{ product.nso_service_id_path }}.nso_service_id)
    {% endif -%}
    {% if product.ims_circuit_id_path %}
    ims_service_id = subscription.{{ product.ims_circuit_id_path }}.ims_circuit_id
    {% endif -%}

    # TODO: optionally add additional values.
    # Copy values to the root of the state for easy access

    return {
        "subscription": subscription,
        {%- if product.nso_service_id_path %}
        NSO_SERVICE_ID: nso_service_id,
        {%- endif %}
        {%- if product.ims_circuit_id_path %}
        "ims_service_id": ims_service_id,
        {%- endif %}
    }

{% if validations -%}
{% for validation in validations %}
def validate_{{ validation.id }}(cls: FormPage, values: dict) -> dict:
    # TODO: add validation for "{{ validation.description }}"
    if True:
        raise ValueError({{ validation.description }})

    return values
{% endfor %}
{% endif -%}

def terminate_initial_input_form_generator(subscription_id: UUIDstr, organisation: UUIDstr) -> InputForm:
    temp_subscription_id = subscription_id

    class TerminateForm(FormPage):
        subscription_id: DisplaySubscription = temp_subscription_id  # type: ignore
{% include "additional_terminate_input_fields.j2" ignore missing -%}

        {% for validation in validations %}
        _check_{{ validation.id }}: classmethod = root_validator(allow_reuse=True)(validate_{{ validation.id }})
        {% endfor %}
    return TerminateForm


{% include 'additional_terminate_steps.j2' %}


@terminate_workflow("Terminate {{ product.name }}", initial_input_form=terminate_initial_input_form_generator, additional_steps=additional_steps)
def terminate_{{ product.variable }}() -> StepList:
    return (
        begin
        >> load_initial_state
        # TODO: fill in additional steps if needed
        {%- if product.config.send_email %}
        >> send_confirmation_email()
        {% endif %}
    )
