"""Add Lesson Templates Table (Feature f37)

Erstellt die lesson_templates Tabelle fuer wiederverwendbare
Stundenkonfigurationen.

Revision ID: 002
Revises: 001
Create Date: 2026-01-15 14:00:00

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

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


def upgrade() -> None:
    op.create_table(
        'lesson_templates',
        sa.Column('id', sa.String(36), primary_key=True),
        sa.Column('teacher_id', sa.String(100), nullable=False, index=True),
        sa.Column('name', sa.String(200), nullable=False),
        sa.Column('description', sa.Text(), default=''),
        sa.Column('subject', sa.String(100), default=''),
        sa.Column('grade_level', sa.String(50), default=''),
        sa.Column('phase_durations', sa.JSON(), default=dict),
        sa.Column('default_topic', sa.String(500), default=''),
        sa.Column('default_notes', sa.Text(), default=''),
        sa.Column('is_public', sa.Boolean(), default=False),
        sa.Column('usage_count', sa.Integer(), default=0),
        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 oeffentliche Vorlagen
    op.create_index(
        'ix_lesson_templates_public',
        'lesson_templates',
        ['is_public', 'usage_count'],
        postgresql_where=sa.text('is_public = true')
    )


def downgrade() -> None:
    op.drop_index('ix_lesson_templates_public', table_name='lesson_templates')
    op.drop_table('lesson_templates')
