"""add academic periods system

Revision ID: 8d1df7199cb7
Revises: 71ba7ab08d84
Create Date: 2025-09-20 11:07:08.059374

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '8d1df7199cb7'
down_revision: Union[str, None] = '71ba7ab08d84'
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('academic_periods',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('name', sa.String(length=100), nullable=False),
    sa.Column('display_name', sa.String(length=50), nullable=True),
    sa.Column('start_date', sa.Date(), nullable=False),
    sa.Column('end_date', sa.Date(), nullable=False),
    sa.Column('period_type', sa.Enum('schuljahr', 'semester', 'trimester', name='academicperiodtype'), nullable=False),
    sa.Column('is_active', sa.Boolean(), nullable=False),
    sa.Column('created_at', sa.TIMESTAMP(timezone=True), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=True),
    sa.Column('updated_at', sa.TIMESTAMP(timezone=True), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('name', name='uq_academic_periods_name')
    )
    op.create_index('ix_academic_periods_active', 'academic_periods', ['is_active'], unique=False)
    op.create_index(op.f('ix_academic_periods_end_date'), 'academic_periods', ['end_date'], unique=False)
    op.create_index(op.f('ix_academic_periods_start_date'), 'academic_periods', ['start_date'], unique=False)
    op.add_column('event_media', sa.Column('academic_period_id', sa.Integer(), nullable=True))
    op.create_index(op.f('ix_event_media_academic_period_id'), 'event_media', ['academic_period_id'], unique=False)
    op.create_foreign_key(None, 'event_media', 'academic_periods', ['academic_period_id'], ['id'])
    op.add_column('events', sa.Column('academic_period_id', sa.Integer(), nullable=True))
    op.create_index(op.f('ix_events_academic_period_id'), 'events', ['academic_period_id'], unique=False)
    op.create_foreign_key(None, 'events', 'academic_periods', ['academic_period_id'], ['id'])
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'events', type_='foreignkey')
    op.drop_index(op.f('ix_events_academic_period_id'), table_name='events')
    op.drop_column('events', 'academic_period_id')
    op.drop_constraint(None, 'event_media', type_='foreignkey')
    op.drop_index(op.f('ix_event_media_academic_period_id'), table_name='event_media')
    op.drop_column('event_media', 'academic_period_id')
    op.drop_index(op.f('ix_academic_periods_start_date'), table_name='academic_periods')
    op.drop_index(op.f('ix_academic_periods_end_date'), table_name='academic_periods')
    op.drop_index('ix_academic_periods_active', table_name='academic_periods')
    op.drop_table('academic_periods')
    # ### end Alembic commands ###
