"""Add Membership access level

Revision ID: e7c6481e972b
Revises: 0fd54220f319
Create Date: 2025-04-25 12:12:50.865109

"""

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 = "e7c6481e972b"
down_revision: str | None = "0fd54220f319"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    sa.Enum("EDITOR", "ADMIN", name="workspacerole").create(op.get_bind())
    op.add_column(
        "membership",
        sa.Column(
            "role",
            postgresql.ENUM("EDITOR", "ADMIN", name="workspacerole", create_type=False),
            nullable=False,
            server_default="EDITOR",
        ),
    )
    op.drop_index("ix_workspace_name", table_name="workspace")
    op.create_index(op.f("ix_workspace_name"), "workspace", ["name"], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f("ix_workspace_name"), table_name="workspace")
    op.create_index("ix_workspace_name", "workspace", ["name"], unique=True)
    op.drop_column("membership", "role")
    sa.Enum("EDITOR", "ADMIN", name="workspacerole").drop(op.get_bind())
    # ### end Alembic commands ###
