"""Initial Classroom Engine Tables (Feature f13/f15)

Erstellt die Tabellen fuer:
- lesson_sessions: Unterrichtsstunden
- lesson_phase_history: Phasen-Verlauf
- teacher_settings: Lehrer-Einstellungen

Revision ID: 001
Revises: None
Create Date: 2026-01-15 12:00:00

"""
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 = '001'
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


# Enum-Typ fuer Unterrichtsphasen
lesson_phase_enum = postgresql.ENUM(
    'not_started',
    'einstieg',
    'erarbeitung',
    'sicherung',
    'transfer',
    'reflexion',
    'ended',
    name='lessonphaseenum',
    create_type=False
)


def upgrade() -> None:
    # Enum-Typ erstellen
    lesson_phase_enum.create(op.get_bind(), checkfirst=True)

    # lesson_sessions Tabelle
    op.create_table(
        'lesson_sessions',
        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('topic', sa.String(500), nullable=True),
        sa.Column(
            'current_phase',
            lesson_phase_enum,
            nullable=False,
            server_default='not_started'
        ),
        sa.Column('is_paused', sa.Boolean(), default=False),
        sa.Column('created_at', sa.DateTime(), server_default=sa.func.now()),
        sa.Column('lesson_started_at', sa.DateTime(), nullable=True),
        sa.Column('lesson_ended_at', sa.DateTime(), nullable=True),
        sa.Column('phase_started_at', sa.DateTime(), nullable=True),
        sa.Column('pause_started_at', sa.DateTime(), nullable=True),
        sa.Column('total_paused_seconds', sa.Integer(), default=0),
        sa.Column('phase_durations', sa.JSON(), default=dict),
        sa.Column('phase_history', sa.JSON(), default=list),
        sa.Column('notes', sa.Text(), default=''),
        sa.Column('homework', sa.Text(), default=''),
    )

    # lesson_phase_history Tabelle
    op.create_table(
        'lesson_phase_history',
        sa.Column('id', sa.String(36), primary_key=True),
        sa.Column(
            'session_id',
            sa.String(36),
            sa.ForeignKey('lesson_sessions.id', ondelete='CASCADE'),
            nullable=False,
            index=True
        ),
        sa.Column('phase', lesson_phase_enum, nullable=False),
        sa.Column('started_at', sa.DateTime(), nullable=False),
        sa.Column('ended_at', sa.DateTime(), nullable=True),
        sa.Column('duration_seconds', sa.Integer(), nullable=True),
        sa.Column('was_extended', sa.Boolean(), default=False),
        sa.Column('extension_minutes', sa.Integer(), default=0),
        sa.Column('pause_count', sa.Integer(), default=0),
        sa.Column('total_pause_seconds', sa.Integer(), default=0),
    )

    # teacher_settings Tabelle
    op.create_table(
        'teacher_settings',
        sa.Column('id', sa.String(36), primary_key=True),
        sa.Column(
            'teacher_id',
            sa.String(100),
            unique=True,
            nullable=False,
            index=True
        ),
        sa.Column('default_phase_durations', sa.JSON(), default=dict),
        sa.Column('audio_enabled', sa.Boolean(), default=True),
        sa.Column('high_contrast', sa.Boolean(), default=False),
        sa.Column('show_statistics', sa.Boolean(), default=True),
        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()
        ),
    )


def downgrade() -> None:
    op.drop_table('teacher_settings')
    op.drop_table('lesson_phase_history')
    op.drop_table('lesson_sessions')

    # Enum-Typ entfernen
    lesson_phase_enum.drop(op.get_bind(), checkfirst=True)
