"""fix topics course_type column

Revision ID: fb7d84c6ad9
Revises: fa7d84c6ad8
Create Date: 2025-09-18 00:00:00.000000

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'fb7d84c6ad9'
down_revision = 'fa7d84c6ad8'
branch_labels = None
depends_on = None


def upgrade():
    # Create a temporary table with the new schema
    op.execute('''
        CREATE TABLE topics_new (
            id INTEGER NOT NULL PRIMARY KEY,
            title VARCHAR(100) NOT NULL,
            category VARCHAR(50) NOT NULL,
            "order" INTEGER,
            reading_content TEXT NOT NULL,
            writing_prompt TEXT,
            speaking_prompt TEXT,
            created_at DATETIME,
            teacher_id INTEGER NOT NULL,
            course_id INTEGER,
            course_type VARCHAR(20),
            FOREIGN KEY(teacher_id) REFERENCES users (id)
        )
    ''')
    
    # Copy data from the old table to the new one
    op.execute('''
        INSERT INTO topics_new (
            id, title, category, "order", reading_content, writing_prompt,
            speaking_prompt, created_at, teacher_id, course_id
        )
        SELECT id, title, category, "order", reading_content, writing_prompt,
               speaking_prompt, created_at, teacher_id, course_id
        FROM topics
    ''')
    
    # Set a default value for course_type
    op.execute('''
        UPDATE topics_new
        SET course_type = 'short'
        WHERE course_id IS NOT NULL
    ''')
    
    # Drop the old table and rename the new one
    op.execute('DROP TABLE topics')
    op.execute('ALTER TABLE topics_new RENAME TO topics')

    # Add any indexes that were on the original table
    op.create_index('ix_topics_created_at', 'topics', ['created_at'])
    op.create_index('ix_topics_teacher_id', 'topics', ['teacher_id'])


def downgrade():
    # Create a temporary table without course_type
    op.execute('''
        CREATE TABLE topics_old (
            id INTEGER NOT NULL PRIMARY KEY,
            title VARCHAR(100) NOT NULL,
            category VARCHAR(50) NOT NULL,
            "order" INTEGER,
            reading_content TEXT NOT NULL,
            writing_prompt TEXT,
            speaking_prompt TEXT,
            created_at DATETIME,
            teacher_id INTEGER NOT NULL,
            course_id INTEGER,
            FOREIGN KEY(teacher_id) REFERENCES users (id)
        )
    ''')
    
    # Copy data back, excluding course_type
    op.execute('''
        INSERT INTO topics_old (
            id, title, category, "order", reading_content, writing_prompt,
            speaking_prompt, created_at, teacher_id, course_id
        )
        SELECT id, title, category, "order", reading_content, writing_prompt,
               speaking_prompt, created_at, teacher_id, course_id
        FROM topics
    ''')
    
    # Drop the new table and rename the old one back
    op.execute('DROP TABLE topics')
    op.execute('ALTER TABLE topics_old RENAME TO topics')

    # Recreate indexes
    op.create_index('ix_topics_created_at', 'topics', ['created_at'])
    op.create_index('ix_topics_teacher_id', 'topics', ['teacher_id'])