"""add registry version and index tables

Revision ID: 4c6310af479d
Revises: 0fd1f09cd98b
Create Date: 2025-12-09 16:56:39.452000

"""

from collections.abc import Sequence

import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "4c6310af479d"
down_revision: str | None = "591905d1205e"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "registry_version",
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("repository_id", sa.UUID(), nullable=False),
        sa.Column("version", sa.String(), nullable=False),
        sa.Column("commit_sha", sa.String(), nullable=True),
        sa.Column("manifest", postgresql.JSONB(astext_type=sa.Text()), nullable=False),
        sa.Column("wheel_uri", sa.String(), nullable=False),
        sa.Column("organization_id", sa.UUID(), nullable=False),
        sa.Column("surrogate_id", sa.Integer(), nullable=False),
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["repository_id"],
            ["registry_repository.id"],
            name=op.f("fk_registry_version_repository_id_registry_repository"),
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("surrogate_id", name=op.f("pk_registry_version")),
        sa.UniqueConstraint(
            "organization_id",
            "repository_id",
            "version",
            name=op.f("uq_registry_version_organization_id_repository_id_version"),
        ),
    )
    op.create_index(
        op.f("ix_registry_version_id"), "registry_version", ["id"], unique=True
    )
    op.create_table(
        "registry_index",
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("registry_version_id", sa.UUID(), nullable=False),
        sa.Column("namespace", sa.String(), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("action_type", sa.String(), nullable=False),
        sa.Column("description", sa.String(), nullable=False),
        sa.Column("default_title", sa.String(), nullable=True),
        sa.Column("display_group", sa.String(), nullable=True),
        sa.Column("doc_url", sa.String(), nullable=True),
        sa.Column("author", sa.String(), nullable=True),
        sa.Column("deprecated", sa.String(), nullable=True),
        sa.Column("secrets", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("interface", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("options", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("organization_id", sa.UUID(), nullable=False),
        sa.Column("surrogate_id", sa.Integer(), nullable=False),
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["registry_version_id"],
            ["registry_version.id"],
            name=op.f("fk_registry_index_registry_version_id_registry_version"),
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("surrogate_id", name=op.f("pk_registry_index")),
        sa.UniqueConstraint(
            "registry_version_id",
            "namespace",
            "name",
            name=op.f("uq_registry_index_registry_version_id_namespace_name"),
        ),
    )
    op.create_index(op.f("ix_registry_index_id"), "registry_index", ["id"], unique=True)
    op.create_index(
        op.f("ix_registry_index_name"), "registry_index", ["name"], unique=False
    )
    op.create_index(
        op.f("ix_registry_index_namespace"),
        "registry_index",
        ["namespace"],
        unique=False,
    )
    op.add_column(
        "workflow",
        sa.Column(
            "registry_lock", postgresql.JSONB(astext_type=sa.Text()), nullable=True
        ),
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column("workflow", "registry_lock")
    op.drop_index(op.f("ix_registry_index_namespace"), table_name="registry_index")
    op.drop_index(op.f("ix_registry_index_name"), table_name="registry_index")
    op.drop_index(op.f("ix_registry_index_id"), table_name="registry_index")
    op.drop_table("registry_index")
    op.drop_index(op.f("ix_registry_version_id"), table_name="registry_version")
    op.drop_table("registry_version")
    # ### end Alembic commands ###
