"""add_chat_session_turn_event

Revision ID: 65900af1312b
Revises: c80fc0e31e4a
Create Date: 2026-03-20 11:22:37.745155

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

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


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('chat_sessions',
    sa.Column('id', sa.UUID(), nullable=False),
    sa.Column('repo_id', sa.UUID(), nullable=False),
    sa.Column('title', sa.String(length=255), nullable=False),
    sa.Column('active_provider', sa.String(length=50), nullable=False),
    sa.Column('active_model', sa.String(length=255), nullable=False),
    sa.Column('seeded_commit_id', sa.UUID(), nullable=True),
    sa.Column('metadata', postgresql.JSONB(astext_type=sa.Text()), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
    sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False),
    sa.ForeignKeyConstraint(['repo_id'], ['repos.id'], ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['seeded_commit_id'], ['commits.id'], ondelete='SET NULL'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_chat_sessions_repo_id'), 'chat_sessions', ['repo_id'], unique=False)
    op.create_table('turn_events',
    sa.Column('id', sa.UUID(), nullable=False),
    sa.Column('session_id', sa.UUID(), nullable=False),
    sa.Column('repo_id', sa.UUID(), nullable=False),
    sa.Column('role', sa.String(length=20), nullable=False),
    sa.Column('content', sa.Text(), nullable=False),
    sa.Column('provider', sa.String(length=50), nullable=False),
    sa.Column('model', sa.String(length=255), nullable=False),
    sa.Column('sequence_number', sa.Integer(), nullable=False),
    sa.Column('commit_id', sa.UUID(), nullable=True),
    sa.Column('metadata', postgresql.JSONB(astext_type=sa.Text()), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
    sa.ForeignKeyConstraint(['commit_id'], ['commits.id'], ondelete='SET NULL'),
    sa.ForeignKeyConstraint(['repo_id'], ['repos.id'], ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['session_id'], ['chat_sessions.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_turn_events_repo_id'), 'turn_events', ['repo_id'], unique=False)
    op.create_index(op.f('ix_turn_events_session_id'), 'turn_events', ['session_id'], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_turn_events_session_id'), table_name='turn_events')
    op.drop_index(op.f('ix_turn_events_repo_id'), table_name='turn_events')
    op.drop_table('turn_events')
    op.drop_index(op.f('ix_chat_sessions_repo_id'), table_name='chat_sessions')
    op.drop_table('chat_sessions')
    # ### end Alembic commands ###
