"""add platform models and organization table

Revision ID: 7aab03def5b6
Revises: f856d9b7ee5a
Create Date: 2026-01-15 22:10:19.223956

"""

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 = "7aab03def5b6"
down_revision: str | None = "f856d9b7ee5a"
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(
        "organization",
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("slug", sa.String(), nullable=False),
        sa.Column("is_active", sa.Boolean(), 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.PrimaryKeyConstraint("id", name=op.f("pk_organization")),
    )
    op.create_index(
        op.f("ix_organization_name"), "organization", ["name"], unique=False
    )
    op.create_index(op.f("ix_organization_slug"), "organization", ["slug"], unique=True)
    op.create_table(
        "platform_registry_repository",
        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.Column("id", sa.UUID(), nullable=False),
        sa.Column("origin", sa.String(), nullable=False),
        sa.Column("last_synced_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("commit_sha", sa.String(), nullable=True),
        sa.PrimaryKeyConstraint(
            "surrogate_id", name=op.f("pk_platform_registry_repository")
        ),
        sa.UniqueConstraint("id", name=op.f("uq_platform_registry_repository_id")),
        sa.UniqueConstraint(
            "origin", name=op.f("uq_platform_registry_repository_origin")
        ),
    )
    op.create_table(
        "platform_secret",
        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.Column("id", sa.UUID(), nullable=False),
        sa.Column("type", sa.String(length=255), nullable=False),
        sa.Column("name", sa.String(length=255), nullable=False),
        sa.Column("description", sa.String(length=255), nullable=True),
        sa.Column("encrypted_keys", sa.LargeBinary(), nullable=False),
        sa.Column("environment", sa.String(length=255), nullable=False),
        sa.Column("tags", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.PrimaryKeyConstraint("surrogate_id", name=op.f("pk_platform_secret")),
        sa.UniqueConstraint(
            "name", "environment", name=op.f("uq_platform_secret_name_environment")
        ),
    )
    op.create_index(
        op.f("ix_platform_secret_id"), "platform_secret", ["id"], unique=True
    )
    op.create_index(
        op.f("ix_platform_secret_name"), "platform_secret", ["name"], unique=False
    )
    op.create_table(
        "platform_settings",
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("key", sa.String(), nullable=False),
        sa.Column("value", sa.LargeBinary(), nullable=False),
        sa.Column("value_type", sa.String(), nullable=False),
        sa.Column(
            "is_encrypted",
            sa.Boolean(),
            server_default=sa.text("false"),
            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.PrimaryKeyConstraint("id", name=op.f("pk_platform_settings")),
    )
    op.create_index(
        op.f("ix_platform_settings_key"), "platform_settings", ["key"], unique=True
    )
    op.create_table(
        "platform_registry_action",
        sa.Column("repository_id", sa.UUID(), nullable=True),
        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.Column("id", sa.UUID(), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("description", sa.String(), nullable=False),
        sa.Column("namespace", sa.String(), nullable=False),
        sa.Column("origin", sa.String(), nullable=False),
        sa.Column("type", 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(
            "implementation", postgresql.JSONB(astext_type=sa.Text()), nullable=True
        ),
        sa.Column("options", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.ForeignKeyConstraint(
            ["repository_id"],
            ["platform_registry_repository.id"],
            name=op.f(
                "fk_platform_registry_action_repository_id_platform_registry_repository"
            ),
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint(
            "surrogate_id", name=op.f("pk_platform_registry_action")
        ),
        sa.UniqueConstraint("id", name=op.f("uq_platform_registry_action_id")),
        sa.UniqueConstraint(
            "namespace", "name", name=op.f("uq_platform_registry_action_namespace_name")
        ),
    )
    op.create_table(
        "platform_registry_version",
        sa.Column("repository_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.Column("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("tarball_uri", sa.String(), nullable=False),
        sa.ForeignKeyConstraint(
            ["repository_id"],
            ["platform_registry_repository.id"],
            name=op.f(
                "fk_platform_registry_version_repository_id_platform_registry_repository"
            ),
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint(
            "surrogate_id", name=op.f("pk_platform_registry_version")
        ),
        sa.UniqueConstraint("id", name=op.f("uq_platform_registry_version_id")),
        sa.UniqueConstraint(
            "repository_id",
            "version",
            name=op.f("uq_platform_registry_version_repository_id_version"),
        ),
    )
    op.create_index(
        op.f("ix_platform_registry_version_id"),
        "platform_registry_version",
        ["id"],
        unique=True,
    )
    op.create_table(
        "platform_registry_index",
        sa.Column("registry_version_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.Column("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.ForeignKeyConstraint(
            ["registry_version_id"],
            ["platform_registry_version.id"],
            name=op.f(
                "fk_platform_registry_index_registry_version_id_platform_registry_version"
            ),
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint(
            "surrogate_id", name=op.f("pk_platform_registry_index")
        ),
        sa.UniqueConstraint("id", name=op.f("uq_platform_registry_index_id")),
        sa.UniqueConstraint(
            "registry_version_id",
            "namespace",
            "name",
            name=op.f("uq_platform_registry_index_registry_version_id_namespace_name"),
        ),
    )
    op.create_index(
        op.f("ix_platform_registry_index_id"),
        "platform_registry_index",
        ["id"],
        unique=True,
    )
    op.create_index(
        op.f("ix_platform_registry_index_name"),
        "platform_registry_index",
        ["name"],
        unique=False,
    )
    op.create_index(
        op.f("ix_platform_registry_index_namespace"),
        "platform_registry_index",
        ["namespace"],
        unique=False,
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(
        op.f("ix_platform_registry_index_namespace"),
        table_name="platform_registry_index",
    )
    op.drop_index(
        op.f("ix_platform_registry_index_name"), table_name="platform_registry_index"
    )
    op.drop_index(
        op.f("ix_platform_registry_index_id"), table_name="platform_registry_index"
    )
    op.drop_table("platform_registry_index")
    op.drop_index(
        op.f("ix_platform_registry_version_id"), table_name="platform_registry_version"
    )
    op.drop_table("platform_registry_version")
    op.drop_table("platform_registry_action")
    op.drop_index(op.f("ix_platform_settings_key"), table_name="platform_settings")
    op.drop_table("platform_settings")
    op.drop_index(op.f("ix_platform_secret_name"), table_name="platform_secret")
    op.drop_index(op.f("ix_platform_secret_id"), table_name="platform_secret")
    op.drop_table("platform_secret")
    op.drop_table("platform_registry_repository")
    op.drop_index(op.f("ix_organization_slug"), table_name="organization")
    op.drop_index(op.f("ix_organization_name"), table_name="organization")
    op.drop_table("organization")
    # ### end Alembic commands ###
