"""Add teacher_contexts, schoolyear_events, recurring_routines tables for Phase 8.

Schuljahres-Begleiter Erweiterung mit 2-Schichten-Modell:
- Makro-Ebene: 7 Schuljahres-Phasen
- Mikro-Ebene: Events, Routinen, Arbeitsmodi

Revision ID: 007_teacher_context
Revises: 006_teacher_feedback
Create Date: 2026-01-15 20:00:00.000000
"""
from alembic import op
import sqlalchemy as sa

# revision identifiers
revision = '007_teacher_context'
down_revision = '006_teacher_feedback'
branch_labels = None
depends_on = None


def upgrade() -> None:
    """Create teacher_contexts, schoolyear_events, recurring_routines tables."""

    # Teacher Context - Makro-State pro Lehrer
    op.create_table(
        'teacher_contexts',
        sa.Column('id', sa.String(36), primary_key=True),
        sa.Column('teacher_id', sa.String(100), unique=True, nullable=False, index=True),
        # Schul-Kontext
        sa.Column('federal_state', sa.String(10), default='BY'),
        sa.Column('school_type', sa.String(50), default='gymnasium'),
        # Schuljahr
        sa.Column('schoolyear', sa.String(20), default='2024-2025'),
        sa.Column('schoolyear_start', sa.DateTime, nullable=True),
        # Makro-Phase
        sa.Column('macro_phase', sa.String(50), nullable=False, default='onboarding'),
        sa.Column('current_week', sa.Integer, default=1),
        # Berechnete Flags
        sa.Column('is_exam_period', sa.Boolean, default=False),
        sa.Column('is_before_holidays', sa.Boolean, default=False),
        # Onboarding-Status
        sa.Column('onboarding_completed', sa.Boolean, default=False),
        sa.Column('has_classes', sa.Boolean, default=False),
        sa.Column('has_schedule', sa.Boolean, default=False),
        # Metadaten
        sa.Column('created_at', sa.DateTime, default=sa.func.now()),
        sa.Column('updated_at', sa.DateTime, default=sa.func.now(), onupdate=sa.func.now()),
    )

    # Schoolyear Events - Einmalige Events (Klausuren, Elternabende, etc.)
    op.create_table(
        'schoolyear_events',
        sa.Column('id', sa.String(36), primary_key=True),
        sa.Column('teacher_id', sa.String(100), nullable=False, index=True),
        # Event-Details
        sa.Column('event_type', sa.String(50), nullable=False, default='other'),
        sa.Column('title', sa.String(300), nullable=False),
        sa.Column('description', sa.Text, default=''),
        # Zeitraum
        sa.Column('start_date', sa.DateTime, nullable=False, index=True),
        sa.Column('end_date', sa.DateTime, nullable=True),
        # Zuordnung
        sa.Column('class_id', sa.String(100), nullable=True, index=True),
        sa.Column('subject', sa.String(100), nullable=True),
        # Status
        sa.Column('status', sa.String(50), nullable=False, default='planned', index=True),
        # Antizipations-Flags
        sa.Column('needs_preparation', sa.Boolean, default=True),
        sa.Column('preparation_done', sa.Boolean, default=False),
        sa.Column('reminder_days_before', sa.Integer, default=7),
        # Flexible Metadaten
        sa.Column('extra_data', sa.JSON, default=dict),
        # Timestamps
        sa.Column('created_at', sa.DateTime, default=sa.func.now()),
        sa.Column('updated_at', sa.DateTime, default=sa.func.now(), onupdate=sa.func.now()),
    )

    # Recurring Routines - Wiederkehrende Aktivitaeten
    op.create_table(
        'recurring_routines',
        sa.Column('id', sa.String(36), primary_key=True),
        sa.Column('teacher_id', sa.String(100), nullable=False, index=True),
        # Routine-Details
        sa.Column('routine_type', sa.String(50), nullable=False, default='other'),
        sa.Column('title', sa.String(300), nullable=False),
        sa.Column('description', sa.Text, default=''),
        # Wiederholung
        sa.Column('recurrence_pattern', sa.String(50), nullable=False, default='weekly'),
        sa.Column('day_of_week', sa.Integer, nullable=True),  # 0=Mo, 6=So
        sa.Column('day_of_month', sa.Integer, nullable=True),  # 1-31
        sa.Column('time_of_day', sa.Time, nullable=True),
        # Dauer
        sa.Column('duration_minutes', sa.Integer, default=60),
        # Aktiv?
        sa.Column('is_active', sa.Boolean, default=True),
        # Gueltigkeit
        sa.Column('valid_from', sa.DateTime, nullable=True),
        sa.Column('valid_until', sa.DateTime, nullable=True),
        # Metadaten
        sa.Column('extra_data', sa.JSON, default=dict),
        # Timestamps
        sa.Column('created_at', sa.DateTime, default=sa.func.now()),
        sa.Column('updated_at', sa.DateTime, default=sa.func.now(), onupdate=sa.func.now()),
    )


def downgrade() -> None:
    """Drop Phase 8 tables."""
    op.drop_table('recurring_routines')
    op.drop_table('schoolyear_events')
    op.drop_table('teacher_contexts')
