"""Initial schema with all models

Revision ID: 3ca2152433c6
Revises: 
Create Date: 2025-08-14 01:05:03.022966

"""

from alembic import op
import sqlalchemy as sa



# revision identifiers, used by Alembic.
revision = '3ca2152433c6'
down_revision = None
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('connections',
    sa.Column('id', sa.String(length=36), nullable=False),
    sa.Column('type', sa.Text(), nullable=False),
    sa.Column('connection_obj', sa.Text(), nullable=False),
    sa.Column('created_at', sa.TIMESTAMP(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.CheckConstraint("type IN ('pg', 'mysql', 'mongo', 'sqlite', 'mssql')", name=op.f('ck_connections_ck_connections_type_allowed')),
    sa.PrimaryKeyConstraint('id', name=op.f('pk_connections'))
    )
    op.create_table('files',
    sa.Column('id', sa.String(length=36), nullable=False),
    sa.Column('name', sa.Text(), nullable=False),
    sa.Column('path', sa.Text(), nullable=False),
    sa.Column('type', sa.Text(), nullable=False),
    sa.Column('size', sa.Integer(), nullable=True),
    sa.Column('uploaded_at', sa.TIMESTAMP(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.PrimaryKeyConstraint('id', name=op.f('pk_files'))
    )
    op.create_table('llm_connections',
    sa.Column('id', sa.String(length=36), nullable=False),
    sa.Column('type', sa.Text(), nullable=True),
    sa.Column('config', sa.Text(), nullable=True),
    sa.Column('created_at', sa.TIMESTAMP(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.PrimaryKeyConstraint('id', name=op.f('pk_llm_connections'))
    )
    op.create_table('notebooks',
    sa.Column('id', sa.String(length=36), nullable=False),
    sa.Column('notebook_name', sa.Text(), nullable=False),
    sa.Column('description', sa.Text(), nullable=True),
    sa.Column('created_at', sa.TIMESTAMP(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.Column('updated_at', sa.TIMESTAMP(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.PrimaryKeyConstraint('id', name=op.f('pk_notebooks'))
    )
    op.create_table('datasets',
    sa.Column('id', sa.String(length=36), nullable=False),
    sa.Column('type', sa.Text(), nullable=False),
    sa.Column('name', sa.String(length=255), nullable=True),
    sa.Column('connection_id', sa.String(length=36), nullable=True),
    sa.Column('created_at', sa.TIMESTAMP(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.CheckConstraint("(type='connection' AND connection_id IS NOT NULL) OR (type='file')", name=op.f('ck_datasets_type_refs')),
    sa.CheckConstraint("type IN ('connection', 'file')", name=op.f('ck_datasets_ck_datasets_type')),
    sa.ForeignKeyConstraint(['connection_id'], ['connections.id'], name=op.f('fk_datasets_connection_id_connections'), ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id', name=op.f('pk_datasets'))
    )
    op.create_table('notebook_connections',
    sa.Column('id', sa.String(length=36), nullable=False),
    sa.Column('notebook_id', sa.String(length=36), nullable=False),
    sa.Column('connection_id', sa.String(length=36), nullable=False),
    sa.Column('created_at', sa.TIMESTAMP(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.ForeignKeyConstraint(['connection_id'], ['connections.id'], name=op.f('fk_notebook_connections_connection_id_connections'), ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['notebook_id'], ['notebooks.id'], name=op.f('fk_notebook_connections_notebook_id_notebooks'), ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id', name=op.f('pk_notebook_connections'))
    )
    op.create_table('notebook_state',
    sa.Column('id', sa.String(length=36), nullable=False),
    sa.Column('notebook_id', sa.String(length=36), nullable=False),
    sa.Column('version_number', sa.Integer(), nullable=False),
    sa.Column('state', sa.JSON(), nullable=False),
    sa.Column('created_at', sa.TIMESTAMP(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.ForeignKeyConstraint(['notebook_id'], ['notebooks.id'], name=op.f('fk_notebook_state_notebook_id_notebooks'), ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id', name=op.f('pk_notebook_state')),
    sa.UniqueConstraint('notebook_id', 'version_number', name='uq_notebook_state_version')
    )
    op.create_table('threads',
    sa.Column('id', sa.String(length=36), nullable=False),
    sa.Column('thread_title', sa.Text(), nullable=True),
    sa.Column('notebook_id', sa.String(length=36), nullable=False),
    sa.Column('created_at', sa.TIMESTAMP(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.ForeignKeyConstraint(['notebook_id'], ['notebooks.id'], name=op.f('fk_threads_notebook_id_notebooks'), ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id', name=op.f('pk_threads'))
    )
    op.create_table('messages',
    sa.Column('id', sa.String(length=36), nullable=False),
    sa.Column('thread_id', sa.String(length=36), nullable=True),
    sa.Column('role', sa.Text(), nullable=False),
    sa.Column('content', sa.Text(), nullable=False),
    sa.Column('tool_call_id', sa.String(length=36), nullable=True),
    sa.Column('metadata', sa.JSON(), nullable=True),
    sa.Column('created_at', sa.TIMESTAMP(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.CheckConstraint("role IN ('user', 'assistant', 'system', 'tool')", name=op.f('ck_messages_ck_messages_role')),
    sa.ForeignKeyConstraint(['thread_id'], ['threads.id'], name=op.f('fk_messages_thread_id_threads'), ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id', name=op.f('pk_messages'))
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('messages')
    op.drop_table('threads')
    op.drop_table('notebook_state')
    op.drop_table('notebook_connections')
    op.drop_table('datasets')
    op.drop_table('notebooks')
    op.drop_table('llm_connections')
    op.drop_table('files')
    op.drop_table('connections')
    # ### end Alembic commands ###

