"""add investigation roles permissions


Revision ID: 6e49acfb3816
Revises: 1098b7a5eabc
Create Date: 2025-09-17 21:46:14.314402

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision: str = '6e49acfb3816'
down_revision: Union[str, None] = '1098b7a5eabc'
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.create_table('investigation_user_roles',
    sa.Column('id', sa.UUID(), nullable=False),
    sa.Column('user_id', sa.UUID(), nullable=False),
    sa.Column('investigation_id', sa.UUID(), nullable=False),
    sa.Column('role', sa.Enum('OWNER', 'EDITOR', 'VIEWER', name='role_enum', create_constraint=True), nullable=False),
    sa.ForeignKeyConstraint(['investigation_id'], ['investigations.id'], onupdate='CASCADE', ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['user_id'], ['profiles.id'], onupdate='CASCADE', ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('user_id', 'investigation_id', name='uq_user_investigation')
    )
    op.create_index('idx_investigation_roles_investigation_id', 'investigation_user_roles', ['investigation_id'], unique=False)
    op.create_index('idx_investigation_roles_user_id', 'investigation_user_roles', ['user_id'], unique=False)
    op.drop_index('idx_investigations_profiles_investigation_id', table_name='investigations_profiles')
    op.drop_index('idx_investigations_profiles_profile_id', table_name='investigations_profiles')
    op.drop_index('projects_profiles_unique_profile_project', table_name='investigations_profiles')
    op.drop_table('investigations_profiles')
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('investigations_profiles',
    sa.Column('id', sa.UUID(), autoincrement=False, nullable=False),
    sa.Column('created_at', postgresql.TIMESTAMP(timezone=True), server_default=sa.text('now()'), autoincrement=False, nullable=True),
    sa.Column('investigation_id', sa.UUID(), autoincrement=False, nullable=True),
    sa.Column('profile_id', sa.UUID(), autoincrement=False, nullable=True),
    sa.Column('role', sa.VARCHAR(), server_default=sa.text("'member'::character varying"), autoincrement=False, nullable=True),
    sa.ForeignKeyConstraint(['investigation_id'], ['investigations.id'], name='investigations_profiles_investigation_id_fkey', onupdate='CASCADE', ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['profile_id'], ['profiles.id'], name='investigations_profiles_profile_id_fkey', onupdate='CASCADE', ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id', name='investigations_profiles_pkey')
    )
    op.create_index('projects_profiles_unique_profile_project', 'investigations_profiles', ['profile_id', 'investigation_id'], unique=False)
    op.create_index('idx_investigations_profiles_profile_id', 'investigations_profiles', ['profile_id'], unique=False)
    op.create_index('idx_investigations_profiles_investigation_id', 'investigations_profiles', ['investigation_id'], unique=False)
    op.drop_index('idx_investigation_roles_user_id', table_name='investigation_user_roles')
    op.drop_index('idx_investigation_roles_investigation_id', table_name='investigation_user_roles')
    op.drop_table('investigation_user_roles')
    # ### end Alembic commands ###
