"""Add namespace redirect table and audit enum values for NAMESPACE_MODIFY_NAME and NAMESPACE_MODIFY_DISPLAY_NAME

Revision ID: db7a6e0d0b9d
Revises: 210586684f86
Create Date: 2023-06-20 17:49:43.585786

"""
from alembic import op
import sqlalchemy as sa
from terrareg.alembic.versions import update_enum

# revision identifiers, used by Alembic.
revision = 'db7a6e0d0b9d'
down_revision = '210586684f86'
branch_labels = None
depends_on = None


old_audit_values = [
    'NAMESPACE_CREATE', 'MODULE_PROVIDER_CREATE', 'MODULE_PROVIDER_DELETE', 'MODULE_PROVIDER_UPDATE_GIT_TAG_FORMAT',
    'MODULE_PROVIDER_UPDATE_GIT_PROVIDER', 'MODULE_PROVIDER_UPDATE_GIT_PATH', 'MODULE_PROVIDER_UPDATE_GIT_CUSTOM_BASE_URL',
    'MODULE_PROVIDER_UPDATE_GIT_CUSTOM_CLONE_URL', 'MODULE_PROVIDER_UPDATE_GIT_CUSTOM_BROWSE_URL', 'MODULE_PROVIDER_UPDATE_VERIFIED',
    'MODULE_VERSION_INDEX', 'MODULE_VERSION_PUBLISH', 'MODULE_VERSION_DELETE', 'USER_GROUP_CREATE', 'USER_GROUP_DELETE', 'USER_GROUP_NAMESPACE_PERMISSION_ADD',
    'USER_GROUP_NAMESPACE_PERMISSION_MODIFY', 'USER_GROUP_NAMESPACE_PERMISSION_DELETE', 'USER_LOGIN'
]
new_audit_values = [
    'NAMESPACE_MODIFY_NAME', 'NAMESPACE_MODIFY_DISPLAY_NAME',
]

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('namespace_redirect',
    sa.Column('id', sa.Integer(), nullable=False, autoincrement=True),
    sa.Column('name', sa.String(length=128), nullable=False),
    sa.Column('namespace_id', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['namespace_id'], ['namespace.id'], name='fk_namespace_redirect_namespace_id_namespace_id', onupdate='CASCADE', ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    update_enum(
        'audit_history', 'action', 'auditaction',
        old_audit_values,
        old_audit_values + new_audit_values
    )
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    update_enum(
        'audit_history', 'action', 'auditaction',
        old_audit_values + new_audit_values,
        old_audit_values
    )
    op.drop_table('namespace_redirect')
    # ### end Alembic commands ###
