"""Add Homework Assignments Table (Feature f20)

Erstellt die homework_assignments Tabelle fuer das
Hausaufgaben-Tracking.

Revision ID: 003
Revises: 002
Create Date: 2026-01-15 16:00:00

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

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


def upgrade() -> None:
    op.create_table(
        'homework_assignments',
        sa.Column('id', sa.String(36), primary_key=True),
        sa.Column('teacher_id', sa.String(100), nullable=False, index=True),
        sa.Column('class_id', sa.String(50), nullable=False, index=True),
        sa.Column('subject', sa.String(100), nullable=False),
        sa.Column('title', sa.String(300), nullable=False),
        sa.Column('description', sa.Text(), default=''),
        sa.Column('session_id', sa.String(36), sa.ForeignKey('lesson_sessions.id'), nullable=True, index=True),
        sa.Column('due_date', sa.DateTime(), nullable=True, index=True),
        sa.Column(
            'status',
            sa.Enum('assigned', 'in_progress', 'completed', 'overdue', name='homeworkstatusenum'),
            default='assigned',
            nullable=False
        ),
        sa.Column('created_at', sa.DateTime(), server_default=sa.func.now()),
        sa.Column('updated_at', sa.DateTime(), server_default=sa.func.now(), onupdate=sa.func.now()),
    )

    # Index fuer anstehende Hausaufgaben
    op.create_index(
        'ix_homework_pending',
        'homework_assignments',
        ['teacher_id', 'status', 'due_date'],
    )


def downgrade() -> None:
    op.drop_index('ix_homework_pending', table_name='homework_assignments')
    op.drop_table('homework_assignments')
    # Enum-Typ entfernen
    op.execute("DROP TYPE IF EXISTS homeworkstatusenum")
