"""add teacher_id to learning_courses

Revision ID: f9e7d84c6ad7
Revises: f8e7d84c6ad6
Create Date: 2025-09-18 00:00:00.000000

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.engine.reflection import Inspector


# revision identifiers, used by Alembic.
revision = 'f9e7d84c6ad7'
down_revision = 'f8e7d84c6ad6'
branch_labels = None
depends_on = None


def upgrade():
    # Get database connection
    connection = op.get_bind()
    inspector = Inspector.from_engine(connection)
    
    # Check if teacher_id column already exists
    columns = [col['name'] for col in inspector.get_columns('learning_courses')]
    if 'teacher_id' not in columns:
        with op.batch_alter_table('learning_courses') as batch_op:
            batch_op.add_column(sa.Column('teacher_id', sa.Integer(), nullable=True))
            batch_op.create_foreign_key(
                'fk_learning_courses_teacher_id',
                'users',
                ['teacher_id'],
                ['id']
            )


def downgrade():
    # Drop the column if it exists
    with op.batch_alter_table('learning_courses') as batch_op:
        batch_op.drop_constraint('fk_learning_courses_teacher_id', type_='foreignkey')
        batch_op.drop_column('teacher_id')