"""Add archive lifecycle fields to academic_periods

Revision ID: a7b8c9d0e1f2
Revises: 910951fd300a
Create Date: 2026-03-31 00:00:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = 'a7b8c9d0e1f2'
down_revision: Union[str, None] = '910951fd300a'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    # Add archive lifecycle fields to academic_periods table
    op.add_column('academic_periods', sa.Column('is_archived', sa.Boolean(), nullable=False, server_default='0'))
    op.add_column('academic_periods', sa.Column('archived_at', sa.TIMESTAMP(timezone=True), nullable=True))
    op.add_column('academic_periods', sa.Column('archived_by', sa.Integer(), nullable=True))
    
    # Add foreign key for archived_by
    op.create_foreign_key(
        'fk_academic_periods_archived_by_users_id',
        'academic_periods',
        'users',
        ['archived_by'],
        ['id'],
        ondelete='SET NULL'
    )
    
    # Add indexes for performance
    op.create_index('ix_academic_periods_archived', 'academic_periods', ['is_archived'])
    op.create_index('ix_academic_periods_name_not_archived', 'academic_periods', ['name', 'is_archived'])


def downgrade() -> None:
    """Downgrade schema."""
    # Drop indexes
    op.drop_index('ix_academic_periods_name_not_archived', 'academic_periods')
    op.drop_index('ix_academic_periods_archived', 'academic_periods')
    
    # Drop foreign key
    op.drop_constraint('fk_academic_periods_archived_by_users_id', 'academic_periods')
    
    # Drop columns
    op.drop_column('academic_periods', 'archived_by')
    op.drop_column('academic_periods', 'archived_at')
    op.drop_column('academic_periods', 'is_archived')
