"""Add Scene and new vertical fields

Revision ID: 4f14be7d0551
Revises: 65c515b4722a
Create Date: 2025-12-29 01:15:06.570017

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
import sqlmodel


# revision identifiers, used by Alembic.
revision: str = '4f14be7d0551'
down_revision: Union[str, Sequence[str], None] = '65c515b4722a'
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('musician',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('slug', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('bio', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('image_url', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('primary_instrument', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('notes', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('musician', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_musician_name'), ['name'], unique=False)
        batch_op.create_index(batch_op.f('ix_musician_slug'), ['slug'], unique=True)

    op.create_table('scene',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('slug', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('description', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('scene', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_scene_name'), ['name'], unique=True)
        batch_op.create_index(batch_op.f('ix_scene_slug'), ['slug'], unique=True)

    op.create_table('sequence',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('slug', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('description', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('notes', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('sequence', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_sequence_name'), ['name'], unique=False)
        batch_op.create_index(batch_op.f('ix_sequence_slug'), ['slug'], unique=True)

    op.create_table('bandmembership',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('musician_id', sa.Integer(), nullable=False),
    sa.Column('artist_id', sa.Integer(), nullable=False),
    sa.Column('role', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('start_date', sa.DateTime(), nullable=True),
    sa.Column('end_date', sa.DateTime(), nullable=True),
    sa.Column('notes', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.ForeignKeyConstraint(['artist_id'], ['artist.id'], ),
    sa.ForeignKeyConstraint(['musician_id'], ['musician.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('reaction',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('entity_type', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('entity_id', sa.Integer(), nullable=False),
    sa.Column('emoji', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('reaction', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_reaction_entity_id'), ['entity_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_reaction_entity_type'), ['entity_type'], unique=False)

    op.create_table('songcanon',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('title', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('slug', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('original_artist', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('original_artist_id', sa.Integer(), nullable=True),
    sa.Column('notes', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.ForeignKeyConstraint(['original_artist_id'], ['artist.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('songcanon', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_songcanon_slug'), ['slug'], unique=True)
        batch_op.create_index(batch_op.f('ix_songcanon_title'), ['title'], unique=False)

    op.create_table('userverticalpreference',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('vertical_id', sa.Integer(), nullable=False),
    sa.Column('display_mode', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('priority', sa.Integer(), nullable=False),
    sa.Column('notify_on_show', sa.Boolean(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
    sa.ForeignKeyConstraint(['vertical_id'], ['vertical.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('userverticalpreference', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_userverticalpreference_user_id'), ['user_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_userverticalpreference_vertical_id'), ['vertical_id'], unique=False)

    op.create_table('verticalscene',
    sa.Column('vertical_id', sa.Integer(), nullable=False),
    sa.Column('scene_id', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['scene_id'], ['scene.id'], ),
    sa.ForeignKeyConstraint(['vertical_id'], ['vertical.id'], ),
    sa.PrimaryKeyConstraint('vertical_id', 'scene_id')
    )
    op.create_table('chasesong',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=False),
    sa.Column('song_id', sa.Integer(), nullable=False),
    sa.Column('priority', sa.Integer(), nullable=False),
    sa.Column('notes', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('caught_at', sa.DateTime(), nullable=True),
    sa.Column('caught_show_id', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['caught_show_id'], ['show.id'], ),
    sa.ForeignKeyConstraint(['song_id'], ['song.id'], ),
    sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('chasesong', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_chasesong_song_id'), ['song_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_chasesong_user_id'), ['user_id'], unique=False)

    op.create_table('sequencesong',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('sequence_id', sa.Integer(), nullable=False),
    sa.Column('song_id', sa.Integer(), nullable=False),
    sa.Column('position', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['sequence_id'], ['sequence.id'], ),
    sa.ForeignKeyConstraint(['song_id'], ['song.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('performanceguest',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('performance_id', sa.Integer(), nullable=False),
    sa.Column('musician_id', sa.Integer(), nullable=False),
    sa.Column('instrument', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('notes', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.ForeignKeyConstraint(['musician_id'], ['musician.id'], ),
    sa.ForeignKeyConstraint(['performance_id'], ['performance.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('artist', schema=None) as batch_op:
        batch_op.add_column(sa.Column('slug', sqlmodel.sql.sqltypes.AutoString(), nullable=False))
        batch_op.add_column(sa.Column('bio', sqlmodel.sql.sqltypes.AutoString(), nullable=True))
        batch_op.add_column(sa.Column('image_url', sqlmodel.sql.sqltypes.AutoString(), nullable=True))
        batch_op.create_index(batch_op.f('ix_artist_slug'), ['slug'], unique=True)

    with op.batch_alter_table('badge', schema=None) as batch_op:
        batch_op.add_column(sa.Column('tier', sqlmodel.sql.sqltypes.AutoString(), nullable=False))
        batch_op.add_column(sa.Column('category', sqlmodel.sql.sqltypes.AutoString(), nullable=False))
        batch_op.add_column(sa.Column('xp_reward', sa.Integer(), nullable=False))

    with op.batch_alter_table('group', schema=None) as batch_op:
        batch_op.add_column(sa.Column('vertical_id', sa.Integer(), nullable=True))
        batch_op.add_column(sa.Column('image_url', sqlmodel.sql.sqltypes.AutoString(), nullable=True))
        batch_op.create_index(batch_op.f('ix_group_vertical_id'), ['vertical_id'], unique=False)
        batch_op.create_foreign_key(None, 'vertical', ['vertical_id'], ['id'])

    with op.batch_alter_table('performance', schema=None) as batch_op:
        batch_op.add_column(sa.Column('bandcamp_link', sqlmodel.sql.sqltypes.AutoString(), nullable=True))
        batch_op.add_column(sa.Column('nugs_link', sqlmodel.sql.sqltypes.AutoString(), nullable=True))

    with op.batch_alter_table('show', schema=None) as batch_op:
        batch_op.add_column(sa.Column('relisten_link', sqlmodel.sql.sqltypes.AutoString(), nullable=True))

    with op.batch_alter_table('song', schema=None) as batch_op:
        batch_op.add_column(sa.Column('canon_id', sa.Integer(), nullable=True))
        batch_op.add_column(sa.Column('artist_id', sa.Integer(), nullable=True))
        batch_op.create_foreign_key(None, 'artist', ['artist_id'], ['id'])
        batch_op.create_foreign_key(None, 'songcanon', ['canon_id'], ['id'])

    with op.batch_alter_table('user', schema=None) as batch_op:
        batch_op.add_column(sa.Column('avatar_bg_color', sqlmodel.sql.sqltypes.AutoString(), nullable=True))
        batch_op.add_column(sa.Column('avatar_text', sqlmodel.sql.sqltypes.AutoString(), nullable=True))
        batch_op.add_column(sa.Column('profile_public', sa.Boolean(), nullable=False))
        batch_op.add_column(sa.Column('show_attendance_public', sa.Boolean(), nullable=False))
        batch_op.add_column(sa.Column('appear_in_leaderboards', sa.Boolean(), nullable=False))

    with op.batch_alter_table('userpreferences', schema=None) as batch_op:
        batch_op.add_column(sa.Column('theme', sqlmodel.sql.sqltypes.AutoString(), nullable=False))
        batch_op.add_column(sa.Column('email_on_reply', sa.Boolean(), nullable=False))
        batch_op.add_column(sa.Column('email_on_chase', sa.Boolean(), nullable=False))
        batch_op.add_column(sa.Column('email_digest', sa.Boolean(), nullable=False))

    with op.batch_alter_table('vertical', schema=None) as batch_op:
        batch_op.add_column(sa.Column('primary_artist_id', sa.Integer(), nullable=True))
        batch_op.add_column(sa.Column('setlistfm_mbid', sqlmodel.sql.sqltypes.AutoString(), nullable=True))
        batch_op.add_column(sa.Column('is_active', sa.Boolean(), nullable=False))
        batch_op.add_column(sa.Column('is_featured', sa.Boolean(), nullable=False))
        batch_op.add_column(sa.Column('logo_url', sqlmodel.sql.sqltypes.AutoString(), nullable=True))
        batch_op.add_column(sa.Column('accent_color', sqlmodel.sql.sqltypes.AutoString(), nullable=True))
        batch_op.create_foreign_key(None, 'artist', ['primary_artist_id'], ['id'])

    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('vertical', schema=None) as batch_op:
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.drop_column('accent_color')
        batch_op.drop_column('logo_url')
        batch_op.drop_column('is_featured')
        batch_op.drop_column('is_active')
        batch_op.drop_column('setlistfm_mbid')
        batch_op.drop_column('primary_artist_id')

    with op.batch_alter_table('userpreferences', schema=None) as batch_op:
        batch_op.drop_column('email_digest')
        batch_op.drop_column('email_on_chase')
        batch_op.drop_column('email_on_reply')
        batch_op.drop_column('theme')

    with op.batch_alter_table('user', schema=None) as batch_op:
        batch_op.drop_column('appear_in_leaderboards')
        batch_op.drop_column('show_attendance_public')
        batch_op.drop_column('profile_public')
        batch_op.drop_column('avatar_text')
        batch_op.drop_column('avatar_bg_color')

    with op.batch_alter_table('song', schema=None) as batch_op:
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.drop_column('artist_id')
        batch_op.drop_column('canon_id')

    with op.batch_alter_table('show', schema=None) as batch_op:
        batch_op.drop_column('relisten_link')

    with op.batch_alter_table('performance', schema=None) as batch_op:
        batch_op.drop_column('nugs_link')
        batch_op.drop_column('bandcamp_link')

    with op.batch_alter_table('group', schema=None) as batch_op:
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.drop_index(batch_op.f('ix_group_vertical_id'))
        batch_op.drop_column('image_url')
        batch_op.drop_column('vertical_id')

    with op.batch_alter_table('badge', schema=None) as batch_op:
        batch_op.drop_column('xp_reward')
        batch_op.drop_column('category')
        batch_op.drop_column('tier')

    with op.batch_alter_table('artist', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_artist_slug'))
        batch_op.drop_column('image_url')
        batch_op.drop_column('bio')
        batch_op.drop_column('slug')

    op.drop_table('performanceguest')
    op.drop_table('sequencesong')
    with op.batch_alter_table('chasesong', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_chasesong_user_id'))
        batch_op.drop_index(batch_op.f('ix_chasesong_song_id'))

    op.drop_table('chasesong')
    op.drop_table('verticalscene')
    with op.batch_alter_table('userverticalpreference', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_userverticalpreference_vertical_id'))
        batch_op.drop_index(batch_op.f('ix_userverticalpreference_user_id'))

    op.drop_table('userverticalpreference')
    with op.batch_alter_table('songcanon', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_songcanon_title'))
        batch_op.drop_index(batch_op.f('ix_songcanon_slug'))

    op.drop_table('songcanon')
    with op.batch_alter_table('reaction', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_reaction_entity_type'))
        batch_op.drop_index(batch_op.f('ix_reaction_entity_id'))

    op.drop_table('reaction')
    op.drop_table('bandmembership')
    with op.batch_alter_table('sequence', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_sequence_slug'))
        batch_op.drop_index(batch_op.f('ix_sequence_name'))

    op.drop_table('sequence')
    with op.batch_alter_table('scene', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_scene_slug'))
        batch_op.drop_index(batch_op.f('ix_scene_name'))

    op.drop_table('scene')
    with op.batch_alter_table('musician', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_musician_slug'))
        batch_op.drop_index(batch_op.f('ix_musician_name'))

    op.drop_table('musician')
    # ### end Alembic commands ###
