"""Add workspace variables

Revision ID: 2d6eadcf4976
Revises: b6c80a9bb5ce
Create Date: 2025-10-26 14:06:11.873927

"""

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 = "2d6eadcf4976"
down_revision: str | None = "b6c80a9bb5ce"
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(
        "workspace_variable",
        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("surrogate_id", sa.Integer(), nullable=False),
        sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("description", sa.String(), nullable=True),
        sa.Column("values", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("environment", sa.String(), nullable=False),
        sa.Column("tags", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("owner_id", sa.UUID(), nullable=False),
        sa.ForeignKeyConstraint(
            ["owner_id"],
            ["workspace.id"],
            ondelete="CASCADE",
            name="workspace_variable_owner_id_fkey",
        ),
        sa.PrimaryKeyConstraint("surrogate_id", name="workspace_variable_pkey"),
        sa.UniqueConstraint(
            "name",
            "environment",
            "owner_id",
            name="workspace_variable_name_environment_owner_id_key",
        ),
    )
    op.create_index(
        op.f("ix_workspace_variable_id"), "workspace_variable", ["id"], unique=True
    )
    op.create_index(
        op.f("ix_workspace_variable_name"), "workspace_variable", ["name"], unique=False
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f("ix_workspace_variable_name"), table_name="workspace_variable")
    op.drop_index(op.f("ix_workspace_variable_id"), table_name="workspace_variable")
    op.drop_table("workspace_variable")
    # ### end Alembic commands ###
