"""Initial migration with all models

Revision ID: 1382bec74309
Revises: 
Create Date: 2025-10-28 17:21:33.097680

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


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


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('environments',
    sa.Column('id', sa.String(), nullable=False),
    sa.Column('organization_id', sa.String(), nullable=False),
    sa.Column('name', sa.String(), nullable=False),
    sa.Column('display_name', sa.String(), nullable=True),
    sa.Column('description', sa.String(), nullable=True),
    sa.Column('tags', sa.JSON(), nullable=False),
    sa.Column('settings', sa.JSON(), nullable=False),
    sa.Column('status', sa.Enum('PENDING', 'PROVISIONING', 'ACTIVE', 'INACTIVE', 'ERROR', name='environmentstatus'), nullable=False),
    sa.Column('worker_token', sa.String(), nullable=True),
    sa.Column('provisioning_workflow_id', sa.String(), nullable=True),
    sa.Column('provisioned_at', sa.DateTime(), nullable=True),
    sa.Column('error_message', sa.String(), nullable=True),
    sa.Column('temporal_namespace_id', sa.String(), nullable=True),
    sa.Column('execution_environment', sa.JSON(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.Column('created_by', sa.String(), nullable=True),
    sa.Column('updated_by', sa.String(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_environments_name'), 'environments', ['name'], unique=False)
    op.create_index(op.f('ix_environments_organization_id'), 'environments', ['organization_id'], unique=False)
    op.create_index(op.f('ix_environments_status'), 'environments', ['status'], unique=False)
    op.create_table('executions',
    sa.Column('id', sa.String(), nullable=False),
    sa.Column('organization_id', sa.String(), nullable=False),
    sa.Column('execution_type', sa.Enum('agent', 'team', 'workflow', name='executiontype'), nullable=False),
    sa.Column('entity_id', sa.String(), nullable=False),
    sa.Column('entity_name', sa.String(), nullable=True),
    sa.Column('runner_name', sa.String(), nullable=True),
    sa.Column('user_id', sa.String(), nullable=True),
    sa.Column('user_email', sa.String(), nullable=True),
    sa.Column('user_name', sa.String(), nullable=True),
    sa.Column('user_avatar', sa.String(), nullable=True),
    sa.Column('prompt', sa.Text(), nullable=False),
    sa.Column('system_prompt', sa.Text(), nullable=True),
    sa.Column('config', sa.JSON(), nullable=True),
    sa.Column('status', sa.Enum('pending', 'running', 'waiting_for_input', 'completed', 'failed', 'cancelled', name='executionstatus'), nullable=False),
    sa.Column('response', sa.Text(), nullable=True),
    sa.Column('error_message', sa.Text(), nullable=True),
    sa.Column('usage', sa.JSON(), nullable=True),
    sa.Column('execution_metadata', sa.JSON(), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('started_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('completed_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_executions_organization_id'), 'executions', ['organization_id'], unique=False)
    op.create_index(op.f('ix_executions_user_id'), 'executions', ['user_id'], unique=False)
    op.create_table('projects',
    sa.Column('id', sa.String(), nullable=False),
    sa.Column('organization_id', sa.String(), nullable=False),
    sa.Column('name', sa.String(), nullable=False),
    sa.Column('description', sa.Text(), nullable=True),
    sa.Column('goals', sa.Text(), nullable=True),
    sa.Column('status', sa.Enum('active', 'archived', 'paused', name='projectstatus'), nullable=False),
    sa.Column('restrict_to_environment', sa.Boolean(), nullable=False),
    sa.Column('default_model', sa.String(), nullable=True),
    sa.Column('default_settings', sa.JSON(), nullable=False),
    sa.Column('is_default', sa.Boolean(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_projects_name'), 'projects', ['name'], unique=False)
    op.create_index(op.f('ix_projects_organization_id'), 'projects', ['organization_id'], unique=False)
    op.create_table('teams',
    sa.Column('id', sa.String(), nullable=False),
    sa.Column('organization_id', sa.String(), nullable=False),
    sa.Column('name', sa.String(), nullable=False),
    sa.Column('description', sa.Text(), nullable=True),
    sa.Column('status', sa.Enum('active', 'inactive', 'archived', name='teamstatus'), nullable=False),
    sa.Column('configuration', sa.JSON(), nullable=False),
    sa.Column('skill_ids', sa.ARRAY(sa.UUID(as_uuid=False)), nullable=False),
    sa.Column('execution_environment', sa.JSON(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('organization_id', 'name', name='uq_team_org_name')
    )
    op.create_index(op.f('ix_teams_name'), 'teams', ['name'], unique=False)
    op.create_index(op.f('ix_teams_organization_id'), 'teams', ['organization_id'], unique=False)
    op.create_table('user_presence',
    sa.Column('id', sa.String(), nullable=False),
    sa.Column('user_id', sa.String(), nullable=False),
    sa.Column('user_email', sa.String(), nullable=True),
    sa.Column('user_name', sa.String(), nullable=True),
    sa.Column('user_avatar', sa.String(), nullable=True),
    sa.Column('agent_id', sa.String(), nullable=True),
    sa.Column('session_id', sa.String(), nullable=True),
    sa.Column('execution_id', sa.String(), nullable=True),
    sa.Column('is_active', sa.Boolean(), nullable=False),
    sa.Column('is_typing', sa.Boolean(), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('last_active_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('idx_presence_lookup', 'user_presence', ['agent_id', 'is_active', 'last_active_at'], unique=False)
    op.create_index('idx_presence_user', 'user_presence', ['user_id', 'is_active'], unique=False)
    op.create_index(op.f('ix_user_presence_agent_id'), 'user_presence', ['agent_id'], unique=False)
    op.create_index(op.f('ix_user_presence_execution_id'), 'user_presence', ['execution_id'], unique=False)
    op.create_index(op.f('ix_user_presence_session_id'), 'user_presence', ['session_id'], unique=False)
    op.create_index(op.f('ix_user_presence_user_id'), 'user_presence', ['user_id'], unique=False)
    op.create_table('agents',
    sa.Column('id', sa.String(), nullable=False),
    sa.Column('name', sa.String(), nullable=False),
    sa.Column('description', sa.Text(), nullable=True),
    sa.Column('status', sa.Enum('idle', 'running', 'paused', 'completed', 'failed', 'stopped', name='agentstatus'), nullable=False),
    sa.Column('capabilities', sa.JSON(), nullable=False),
    sa.Column('configuration', sa.JSON(), nullable=False),
    sa.Column('model_id', sa.String(), nullable=True),
    sa.Column('model_config', sa.JSON(), nullable=False),
    sa.Column('runtime', sa.Enum('default', 'claude_code', name='runtimetype'), server_default='default', nullable=False),
    sa.Column('organization_id', sa.String(), nullable=False),
    sa.Column('team_id', sa.String(), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.Column('last_active_at', sa.DateTime(), nullable=True),
    sa.Column('state', sa.JSON(), nullable=False),
    sa.Column('error_message', sa.Text(), nullable=True),
    sa.ForeignKeyConstraint(['team_id'], ['teams.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_agents_name'), 'agents', ['name'], unique=False)
    op.create_index(op.f('ix_agents_organization_id'), 'agents', ['organization_id'], unique=False)
    op.create_index(op.f('ix_agents_runtime'), 'agents', ['runtime'], unique=False)
    op.create_table('team_environments',
    sa.Column('id', sa.String(), nullable=False),
    sa.Column('team_id', sa.String(), nullable=False),
    sa.Column('environment_id', sa.String(), nullable=False),
    sa.Column('organization_id', sa.String(), nullable=False),
    sa.Column('assigned_at', sa.DateTime(), nullable=False),
    sa.Column('assigned_by', sa.String(), nullable=True),
    sa.ForeignKeyConstraint(['environment_id'], ['environments.id'], ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['team_id'], ['teams.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_team_environments_organization_id'), 'team_environments', ['organization_id'], unique=False)
    op.create_table('workflows',
    sa.Column('id', sa.String(), nullable=False),
    sa.Column('name', sa.String(), nullable=False),
    sa.Column('description', sa.Text(), nullable=True),
    sa.Column('status', sa.Enum('pending', 'running', 'paused', 'completed', 'failed', 'cancelled', name='workflowstatus'), nullable=False),
    sa.Column('steps', sa.JSON(), nullable=False),
    sa.Column('current_step', sa.String(), nullable=True),
    sa.Column('configuration', sa.JSON(), nullable=False),
    sa.Column('team_id', sa.String(), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.Column('started_at', sa.DateTime(), nullable=True),
    sa.Column('completed_at', sa.DateTime(), nullable=True),
    sa.Column('state', sa.JSON(), nullable=False),
    sa.Column('error_message', sa.Text(), nullable=True),
    sa.ForeignKeyConstraint(['team_id'], ['teams.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_workflows_name'), 'workflows', ['name'], unique=False)
    op.create_table('agent_environments',
    sa.Column('id', sa.String(), nullable=False),
    sa.Column('agent_id', sa.String(), nullable=False),
    sa.Column('environment_id', sa.String(), nullable=False),
    sa.Column('organization_id', sa.String(), nullable=False),
    sa.Column('assigned_at', sa.DateTime(), nullable=False),
    sa.Column('assigned_by', sa.String(), nullable=True),
    sa.ForeignKeyConstraint(['agent_id'], ['agents.id'], ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['environment_id'], ['environments.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_agent_environments_organization_id'), 'agent_environments', ['organization_id'], unique=False)
    op.create_table('sessions',
    sa.Column('id', sa.String(), nullable=False),
    sa.Column('agent_id', sa.String(), nullable=False),
    sa.Column('user_id', sa.String(), nullable=True),
    sa.Column('user_email', sa.String(), nullable=True),
    sa.Column('user_name', sa.String(), nullable=True),
    sa.Column('user_avatar', sa.String(), nullable=True),
    sa.Column('messages', sa.JSON(), nullable=False),
    sa.Column('context', sa.JSON(), nullable=False),
    sa.Column('session_metadata', sa.JSON(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.Column('expires_at', sa.DateTime(), nullable=True),
    sa.Column('last_active_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['agent_id'], ['agents.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_sessions_user_id'), 'sessions', ['user_id'], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_sessions_user_id'), table_name='sessions')
    op.drop_table('sessions')
    op.drop_index(op.f('ix_agent_environments_organization_id'), table_name='agent_environments')
    op.drop_table('agent_environments')
    op.drop_index(op.f('ix_workflows_name'), table_name='workflows')
    op.drop_table('workflows')
    op.drop_index(op.f('ix_team_environments_organization_id'), table_name='team_environments')
    op.drop_table('team_environments')
    op.drop_index(op.f('ix_agents_runtime'), table_name='agents')
    op.drop_index(op.f('ix_agents_organization_id'), table_name='agents')
    op.drop_index(op.f('ix_agents_name'), table_name='agents')
    op.drop_table('agents')
    op.drop_index(op.f('ix_user_presence_user_id'), table_name='user_presence')
    op.drop_index(op.f('ix_user_presence_session_id'), table_name='user_presence')
    op.drop_index(op.f('ix_user_presence_execution_id'), table_name='user_presence')
    op.drop_index(op.f('ix_user_presence_agent_id'), table_name='user_presence')
    op.drop_index('idx_presence_user', table_name='user_presence')
    op.drop_index('idx_presence_lookup', table_name='user_presence')
    op.drop_table('user_presence')
    op.drop_index(op.f('ix_teams_organization_id'), table_name='teams')
    op.drop_index(op.f('ix_teams_name'), table_name='teams')
    op.drop_table('teams')
    op.drop_index(op.f('ix_projects_organization_id'), table_name='projects')
    op.drop_index(op.f('ix_projects_name'), table_name='projects')
    op.drop_table('projects')
    op.drop_index(op.f('ix_executions_user_id'), table_name='executions')
    op.drop_index(op.f('ix_executions_organization_id'), table_name='executions')
    op.drop_table('executions')
    op.drop_index(op.f('ix_environments_status'), table_name='environments')
    op.drop_index(op.f('ix_environments_organization_id'), table_name='environments')
    op.drop_index(op.f('ix_environments_name'), table_name='environments')
    op.drop_table('environments')
    # ### end Alembic commands ###
