{# Product block definition -#}

{% from 'macros.j2' import lifecycle_type, required, default, list_field with context -%}

{% for namespace, type in types_to_import %}
from {{ namespace }} import {{ type }}
{%- endfor %}
{% if lists_to_generate %}
from typing import TypeVar
{% endif -%}
{% if constrained_ints_to_generate %}
from typing import Annotated
from annotated_types import Ge, Le
{% endif -%}
{% if "UUID" in fields | map(attribute="type") %}
from uuid import UUID
{% endif %}
{% for module, type in product_blocks_to_import %}
from {{ module }} import {{ type }}BlockInactive, {{ type }}BlockProvisioning, {{ type }}Block
{% endfor %}
from pydantic import computed_field
from orchestrator.domain.base import ProductBlockModel
from orchestrator.types import SubscriptionLifecycle

{% include "enums.j2" %}
{% include "list_definitions.j2" %}
{% include "constrained_int_definitions.j2" %}

class {{ product_block.type }}BlockInactive(ProductBlockModel, product_block_name="{{ product_block.block_name }}"):
    {% for field in fields -%}
    {% if field.type == "list" -%}
    {{ list_field(field.name, lifecycle_type(field.list_type, product_block_types, "Inactive")) }}
    {% else -%}
    {% set type = lifecycle_type(field.type, product_block_types, "Inactive") -%}
    {{ field.name }}: {{ required(field, type, ["inactive"]) }} = {{ default(field) }}
    {% endif -%}
    {% endfor %}

class {{ product_block.type }}BlockProvisioning({{ product_block.type }}BlockInactive, lifecycle=[SubscriptionLifecycle.PROVISIONING]):
    {% for field in fields -%}
    {% if field.type == "list" -%}
    {{ list_field(field.name, lifecycle_type(field.list_type, product_block_types, "Provisioning")) }}
    {% else -%}
    {% set type = lifecycle_type(field.type, product_block_types, "Provisioning") -%}
    {{ field.name }}: {{ required(field, type, ["inactive", "provisioning"]) }}
    {% endif -%}
    {% endfor %}
    @computed_field
    @property
    def title(self) -> str:
        # TODO: format correct title string
        return f"{self.name}"


class {{ product_block.type }}Block({{ product_block.type }}BlockProvisioning, lifecycle=[SubscriptionLifecycle.ACTIVE]):
    {% for field in fields -%}
    {% if field.type == "list" -%}
    {{ list_field(field.name, lifecycle_type(field.list_type, product_block_types, "")) }}
    {% else -%}
    {% set type = lifecycle_type(field.type, product_block_types, "") -%}
    {{ field.name }}: {{ required(field, type, ["inactive", "provisioning", "active"]) }}
    {% endif -%}
    {% endfor %}
