"""Changed user role enums

Revision ID: 147509a82af8
Revises: 4d9291a3acb1
Create Date: 2026-01-14 17:42:10.143289

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '147509a82af8'
down_revision: Union[str, Sequence[str], None] = '4d9291a3acb1'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.execute("CREATE TYPE contactrole_new AS ENUM ('ADMIN', 'MANAGER', 'USER')")
    op.execute("ALTER TABLE contact ALTER COLUMN contact_role DROP DEFAULT")
    op.execute(
        "ALTER TABLE contact ALTER COLUMN contact_role TYPE contactrole_new "
        "USING (CASE "
        "WHEN contact_role::text IN ('ADMIN', 'MANAGER') THEN contact_role::text "
        "ELSE 'USER' "
        "END)::contactrole_new"
    )
    op.execute("ALTER TABLE contact ALTER COLUMN contact_role SET DEFAULT 'USER'")
    op.execute("DROP TYPE contactrole")
    op.execute("ALTER TYPE contactrole_new RENAME TO contactrole")
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.execute("CREATE TYPE contactrole_old AS ENUM ('ADMIN', 'ACCOUNT_ADMIN', 'MANAGER', 'VIEWER')")
    op.execute("ALTER TABLE contact ALTER COLUMN contact_role DROP DEFAULT")
    op.execute(
        "ALTER TABLE contact ALTER COLUMN contact_role TYPE contactrole_old "
        "USING (CASE "
        "WHEN contact_role::text IN ('ADMIN', 'MANAGER') THEN contact_role::text "
        "ELSE 'VIEWER' "
        "END)::contactrole_old"
    )
    op.execute("ALTER TABLE contact ALTER COLUMN contact_role SET DEFAULT 'VIEWER'")
    op.execute("DROP TYPE contactrole")
    op.execute("ALTER TYPE contactrole_old RENAME TO contactrole")
    # ### end Alembic commands ###
