"""Add {{ product.variable }} product.

Revision ID: {{ revision }}
Revises: {{ down_revision }}
Create Date: {{ create_date}}

"""
from uuid import uuid4

from alembic import op

from orchestrator.migrations.helpers import create, create_workflow, delete, delete_workflow, ensure_default_workflows
from orchestrator.targets import Target

# revision identifiers, used by Alembic.
revision = "{{ revision }}"
down_revision = "{{ down_revision }}"
branch_labels = None
depends_on = None

new_products = {
    "products": {
        {% for variant_name, variant_fixed_inputs in product_variants -%}
        "{{ variant_name }}": {
            "product_id": uuid4(),
            "product_type": "{{ product.type }}",
            "description": "{{ product.description }}",
            "tag": "{{ product.tag }}",
            "status": "active",
            "root_product_block": "{{ product.root_product_block }}",
            "fixed_inputs": {
            {%- for fixed_input_type, fixed_input_value in variant_fixed_inputs %}
                "{{ fixed_input_type }}": "{{ fixed_input_value }}",
            {%- endfor %}
            },
        },
        {% endfor -%}
    },
    "product_blocks": {
    {% for pb in product.product_blocks -%}
        "{{ pb.type }}": {
            "product_block_id": uuid4(),
            "description": "{{ pb.description }}",
            "tag": "{{ pb.tag }}",
            "status": "active",
            "resources": {
            {%- for field in pb.resource_types %}
                "{{ field.name }}": "{{ field.description }}",
            {%- endfor %}
            },
            "depends_on_block_relations": [
            {%- for depends_on_block in pb.depends_on_blocks %}
                "{{ depends_on_block }}",
            {%- endfor %}
            ],
        },
    {% endfor -%}
    },
    "workflows": {},
}

new_workflows = [
    {
        "name": "create_{{ product.variable }}",
        "target": Target.CREATE,
        "description": "Create {{ product.name }}",
        "product_type": "{{ product.type }}",
    },
    {
        "name": "modify_{{ product.variable }}",
        "target": Target.MODIFY,
        "description": "Modify {{ product.name }}",
        "product_type": "{{ product.type }}",
    },
    {
        "name": "terminate_{{ product.variable }}",
        "target": Target.TERMINATE,
        "description": "Terminate {{ product.name }}",
        "product_type": "{{ product.type }}",
    },
    {
        "name": "validate_{{ product.variable }}",
        "target": Target.SYSTEM,
        "description": "Validate {{ product.name }}",
        "product_type": "{{ product.type }}",
    },
]


def upgrade() -> None:
    conn = op.get_bind()
    create(conn, new_products)
    for workflow in new_workflows:
        create_workflow(conn, workflow)
    ensure_default_workflows(conn)


def downgrade() -> None:
    conn = op.get_bind()
    for workflow in new_workflows:
        delete_workflow(conn, workflow["name"])

    delete(conn, new_products)
