"""add ai_session_id to script_generations

Revision ID: 3266dd9d8111
Revises: a0b871cb0c5e
Create Date: 2026-03-19 03:52:09.457961

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

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


def upgrade() -> None:
    op.add_column('script_generations', sa.Column(
        'ai_session_id', sa.UUID(), nullable=True,
        comment='FlowPilot AI session that triggered this generation',
    ))
    op.create_index(
        op.f('ix_script_generations_ai_session_id'),
        'script_generations', ['ai_session_id'], unique=False,
    )
    op.create_foreign_key(
        'fk_script_generations_ai_session_id',
        'script_generations', 'ai_sessions',
        ['ai_session_id'], ['id'],
        ondelete='SET NULL',
    )


def downgrade() -> None:
    op.drop_constraint('fk_script_generations_ai_session_id', 'script_generations', type_='foreignkey')
    op.drop_index(op.f('ix_script_generations_ai_session_id'), table_name='script_generations')
    op.drop_column('script_generations', 'ai_session_id')
