"""initial schema

Revision ID: 1f61d287e423
Revises:
Create Date: 2025-12-13 20:09:32.759391

"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "1f61d287e423"
down_revision = None
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "campaign",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("proposal", sa.String(), nullable=False),
        sa.Column("saf", sa.String(), nullable=True),
        sa.Column("meta_data", sa.JSON(), nullable=True),
        sa.Column(
            "create_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "modified_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("name", "proposal", name="uq_campaign_name_proposal"),
    )
    op.create_table(
        "process_template",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("version", sa.String(), nullable=False),
        sa.Column("is_active", sa.Boolean(), nullable=False),
        sa.Column(
            "create_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "modified_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("name"),
        sa.UniqueConstraint("name", "version", name="uq_process_template_name_version"),
    )
    op.create_table(
        "resource_template",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("slug", sa.String(), nullable=True),
        sa.Column("version", sa.String(), nullable=False),
        sa.Column("parent_id", sa.Uuid(), nullable=True),
        sa.Column(
            "create_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "modified_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["parent_id"],
            ["resource_template.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "parent_id",
            "name",
            "version",
            name="uq_resource_template_parent_name_version",
        ),
    )
    op.create_table(
        "resource_type",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column(
            "create_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "modified_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("name"),
    )
    op.create_table(
        "process_run",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("description", sa.String(), nullable=False),
        sa.Column("process_template_id", sa.Uuid(), nullable=False),
        sa.Column("campaign_id", sa.Uuid(), nullable=False),
        sa.Column(
            "create_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "modified_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["campaign_id"],
            ["campaign.id"],
        ),
        sa.ForeignKeyConstraint(
            ["process_template_id"],
            ["process_template.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("name"),
    )
    op.create_table(
        "resource",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("slug", sa.String(), nullable=True),
        sa.Column("active", sa.Boolean(), nullable=False),
        sa.Column("resource_template_id", sa.Uuid(), nullable=True),
        sa.Column("parent_id", sa.Uuid(), nullable=True),
        sa.Column(
            "create_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "modified_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["parent_id"],
            ["resource.id"],
        ),
        sa.ForeignKeyConstraint(
            ["resource_template_id"],
            ["resource_template.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("parent_id", "name", name="uq_resource_parent_name"),
    )
    op.create_index(op.f("ix_resource_slug"), "resource", ["slug"], unique=False)
    op.create_table(
        "resource_slot",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("process_template_id", sa.Uuid(), nullable=False),
        sa.Column("resource_type_id", sa.Uuid(), nullable=False),
        sa.Column(
            "direction",
            sa.Enum("input", "output", name="direction_enum"),
            nullable=False,
        ),
        sa.Column(
            "create_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "modified_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["process_template_id"],
            ["process_template.id"],
        ),
        sa.ForeignKeyConstraint(
            ["resource_type_id"],
            ["resource_type.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "process_template_id", "name", name="uq_process_template_name"
        ),
    )
    op.create_table(
        "resource_template_type_association",
        sa.Column("resource_template_id", sa.Uuid(), nullable=False),
        sa.Column("resource_type_id", sa.Uuid(), nullable=False),
        sa.ForeignKeyConstraint(
            ["resource_template_id"],
            ["resource_template.id"],
        ),
        sa.ForeignKeyConstraint(
            ["resource_type_id"],
            ["resource_type.id"],
        ),
        sa.PrimaryKeyConstraint("resource_template_id", "resource_type_id"),
    )
    op.create_table(
        "step_template",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("process_template_id", sa.Uuid(), nullable=False),
        sa.Column(
            "create_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "modified_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["process_template_id"],
            ["process_template.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "process_template_id", "name", name="uq_step_name_per_process"
        ),
    )
    op.create_index(
        op.f("ix_step_template_process_template_id"),
        "step_template",
        ["process_template_id"],
        unique=False,
    )
    op.create_table(
        "attribute_group_template",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("slug", sa.String(), nullable=True),
        sa.Column("resource_template_id", sa.Uuid(), nullable=True),
        sa.Column("step_template_id", sa.Uuid(), nullable=True),
        sa.Column(
            "create_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "modified_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.CheckConstraint(
            "(resource_template_id IS NOT NULL) <> (step_template_id IS NOT NULL)",
            name="ck_attr_group_exactly_one_owner",
        ),
        sa.ForeignKeyConstraint(
            ["resource_template_id"], ["resource_template.id"], ondelete="CASCADE"
        ),
        sa.ForeignKeyConstraint(
            ["step_template_id"], ["step_template.id"], ondelete="CASCADE"
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "resource_template_id", "name", name="uq_attr_group_name_per_resource"
        ),
        sa.UniqueConstraint(
            "step_template_id", "name", name="uq_attr_group_name_per_step"
        ),
    )
    op.create_index(
        op.f("ix_attribute_group_template_resource_template_id"),
        "attribute_group_template",
        ["resource_template_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_attribute_group_template_step_template_id"),
        "attribute_group_template",
        ["step_template_id"],
        unique=False,
    )
    op.create_table(
        "step",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("process_run_id", sa.Uuid(), nullable=False),
        sa.Column("step_template_id", sa.Uuid(), nullable=False),
        sa.Column(
            "updated_at",
            sa.DateTime(),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "state",
            sa.Enum("PENDING", "IN_PROGRESS", "COMPLETE", name="stepstatus"),
            nullable=False,
        ),
        sa.Column("parent_id", sa.Uuid(), nullable=True),
        sa.Column(
            "create_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "modified_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["parent_id"],
            ["step.id"],
        ),
        sa.ForeignKeyConstraint(
            ["process_run_id"],
            ["process_run.id"],
        ),
        sa.ForeignKeyConstraint(
            ["step_template_id"],
            ["step_template.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_step_parent_id"), "step", ["parent_id"], unique=False)
    op.create_table(
        "step_template_edge",
        sa.Column("process_template_id", sa.Uuid(), nullable=False),
        sa.Column("from_id", sa.Uuid(), nullable=False),
        sa.Column("to_id", sa.Uuid(), nullable=False),
        sa.Column(
            "create_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "modified_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["from_id"],
            ["step_template.id"],
        ),
        sa.ForeignKeyConstraint(
            ["process_template_id"],
            ["process_template.id"],
        ),
        sa.ForeignKeyConstraint(
            ["to_id"],
            ["step_template.id"],
        ),
        sa.PrimaryKeyConstraint("process_template_id", "from_id", "to_id"),
    )
    op.create_table(
        "step_template_resource_slot_binding",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("step_template_id", sa.Uuid(), nullable=False),
        sa.Column("resource_slot_id", sa.Uuid(), nullable=False),
        sa.Column("role", sa.String(), nullable=False),
        sa.Column(
            "create_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "modified_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["resource_slot_id"],
            ["resource_slot.id"],
        ),
        sa.ForeignKeyConstraint(
            ["step_template_id"],
            ["step_template.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("step_template_id", "role", name="uq_step_template_role"),
    )
    op.create_table(
        "attribute_template",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("slug", sa.String(), nullable=True),
        sa.Column("value_type", sa.String(), nullable=False),
        sa.Column("unit", sa.String(), nullable=True),
        sa.Column("default_value", sa.String(), nullable=True),
        sa.Column("metadata", sa.JSON(), nullable=True),
        sa.Column("attribute_group_template_id", sa.Uuid(), nullable=False),
        sa.Column(
            "create_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "modified_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["attribute_group_template_id"],
            ["attribute_group_template.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "parameter",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("step_id", sa.Uuid(), nullable=False),
        sa.Column("attribute_group_template_id", sa.Uuid(), nullable=False),
        sa.Column(
            "create_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "modified_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["attribute_group_template_id"],
            ["attribute_group_template.id"],
        ),
        sa.ForeignKeyConstraint(
            ["step_id"],
            ["step.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "property",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("resource_id", sa.Uuid(), nullable=False),
        sa.Column("attribute_group_template_id", sa.Uuid(), nullable=False),
        sa.Column(
            "create_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "modified_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["attribute_group_template_id"],
            ["attribute_group_template.id"],
        ),
        sa.ForeignKeyConstraint(
            ["resource_id"],
            ["resource.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "resource_assignment",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("process_run_id", sa.Uuid(), nullable=False),
        sa.Column("resource_slot_id", sa.Uuid(), nullable=False),
        sa.Column("step_id", sa.Uuid(), nullable=True),
        sa.Column("resource_id", sa.Uuid(), nullable=False),
        sa.Column(
            "create_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "modified_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["process_run_id"],
            ["process_run.id"],
        ),
        sa.ForeignKeyConstraint(
            ["resource_id"],
            ["resource.id"],
        ),
        sa.ForeignKeyConstraint(
            ["resource_slot_id"],
            ["resource_slot.id"],
        ),
        sa.ForeignKeyConstraint(
            ["step_id"],
            ["step.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "process_run_id", "resource_slot_id", "step_id", name="uq_run_slot_step"
        ),
    )
    op.create_table(
        "step_edge",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("process_id", sa.Uuid(), nullable=False),
        sa.Column("from_step_id", sa.Uuid(), nullable=False),
        sa.Column("to_step_id", sa.Uuid(), nullable=False),
        sa.Column(
            "create_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "modified_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["from_step_id"],
            ["step.id"],
        ),
        sa.ForeignKeyConstraint(
            ["process_id"],
            ["process_run.id"],
        ),
        sa.ForeignKeyConstraint(
            ["to_step_id"],
            ["step.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_table(
        "attribute_value",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column("attribute_template_id", sa.Uuid(), nullable=False),
        sa.Column("parameter_id", sa.Uuid(), nullable=True),
        sa.Column("property_id", sa.Uuid(), nullable=True),
        sa.Column("value", sa.JSON(), nullable=True),
        sa.Column("metadata", sa.JSON(), nullable=True),
        sa.Column(
            "create_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "modified_date",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["attribute_template_id"],
            ["attribute_template.id"],
        ),
        sa.ForeignKeyConstraint(
            ["parameter_id"],
            ["parameter.id"],
        ),
        sa.ForeignKeyConstraint(
            ["property_id"],
            ["property.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("attribute_value")
    op.drop_table("step_edge")
    op.drop_table("resource_assignment")
    op.drop_table("property")
    op.drop_table("parameter")
    op.drop_table("attribute_template")
    op.drop_table("step_template_resource_slot_binding")
    op.drop_table("step_template_edge")
    op.drop_index(op.f("ix_step_parent_id"), table_name="step")
    op.drop_table("step")
    op.drop_index(
        op.f("ix_attribute_group_template_step_template_id"),
        table_name="attribute_group_template",
    )
    op.drop_index(
        op.f("ix_attribute_group_template_resource_template_id"),
        table_name="attribute_group_template",
    )
    op.drop_table("attribute_group_template")
    op.drop_index(
        op.f("ix_step_template_process_template_id"), table_name="step_template"
    )
    op.drop_table("step_template")
    op.drop_table("resource_template_type_association")
    op.drop_table("resource_slot")
    op.drop_index(op.f("ix_resource_slug"), table_name="resource")
    op.drop_table("resource")
    op.drop_table("process_run")
    op.drop_table("resource_type")
    op.drop_table("resource_template")
    op.drop_table("process_template")
    op.drop_table("campaign")
    # ### end Alembic commands ###
