"""init schema

Revision ID: 3196ef00c0b8
Revises: 
Create Date: 2026-04-02 00:22:25.946324

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '3196ef00c0b8'
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('agents',
    sa.Column('uuid', sa.UUID(), nullable=False),
    sa.Column('org_id', sa.UUID(), nullable=False, comment='Organization ID'),
    sa.Column('team_id', sa.UUID(), nullable=False),
    sa.Column('user_id', sa.UUID(), nullable=False, comment='User who created the agent'),
    sa.Column('name', sa.String(), nullable=False),
    sa.Column('type', sa.Integer(), nullable=False, comment='Type of the agent, 1: CLAUDE'),
    sa.Column('description', sa.String(), nullable=True),
    sa.Column('meta', sa.JSON(), nullable=True, comment='Additional metadata for the agent (e.g. config)'),
    sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('is_del', sa.Integer(), nullable=True, comment='0 for not deleted, 1 for deleted'),
    sa.PrimaryKeyConstraint('uuid')
    )
    op.create_table('datasets',
    sa.Column('uuid', sa.UUID(), nullable=False),
    sa.Column('name', sa.String(), nullable=False),
    sa.Column('description', sa.String(), nullable=True),
    sa.Column('meta', sa.JSON(), nullable=True, comment='Additional metadata for the dataset'),
    sa.Column('org_id', sa.UUID(), nullable=False, comment='Organization ID'),
    sa.Column('team_id', sa.UUID(), nullable=False),
    sa.Column('experiment_id', sa.UUID(), nullable=True),
    sa.Column('run_id', sa.UUID(), nullable=True),
    sa.Column('user_id', sa.UUID(), nullable=False, comment='User who created the dataset'),
    sa.Column('path', sa.String(), nullable=False, comment='Storage path for the dataset'),
    sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('is_del', sa.Integer(), nullable=True, comment='0 for not deleted, 1 for deleted'),
    sa.PrimaryKeyConstraint('uuid')
    )
    op.create_table('experiment_labels',
    sa.Column('uuid', sa.UUID(), nullable=False),
    sa.Column('org_id', sa.UUID(), nullable=False, comment='Organization ID'),
    sa.Column('team_id', sa.UUID(), nullable=False),
    sa.Column('experiment_id', sa.UUID(), nullable=False),
    sa.Column('label_name', sa.String(), nullable=False),
    sa.Column('label_value', sa.String(), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
    sa.PrimaryKeyConstraint('uuid')
    )
    op.create_index('idx_experiment_label_lookup', 'experiment_labels', ['experiment_id', 'label_name', 'label_value'], unique=False)
    op.create_table('experiment_tags',
    sa.Column('uuid', sa.UUID(), nullable=False),
    sa.Column('org_id', sa.UUID(), nullable=False, comment='Organization ID'),
    sa.Column('team_id', sa.UUID(), nullable=False),
    sa.Column('experiment_id', sa.UUID(), nullable=False),
    sa.Column('tag', sa.String(), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
    sa.PrimaryKeyConstraint('uuid')
    )
    op.create_index('idx_experiment_tag_lookup', 'experiment_tags', ['experiment_id', 'tag'], unique=False)
    op.create_index('idx_experiment_tag_team', 'experiment_tags', ['team_id', 'tag'], unique=False)
    op.create_table('experiments',
    sa.Column('uuid', sa.UUID(), nullable=False),
    sa.Column('org_id', sa.UUID(), nullable=False, comment='Organization ID'),
    sa.Column('team_id', sa.UUID(), nullable=False),
    sa.Column('user_id', sa.UUID(), nullable=True, comment='User who created the experiment'),
    sa.Column('name', sa.String(), nullable=False),
    sa.Column('description', sa.String(), nullable=True),
    sa.Column('meta', sa.JSON(), nullable=True, comment='Additional metadata for the experiment'),
    sa.Column('params', sa.JSON(), nullable=True, comment='Parameters for the experiment'),
    sa.Column('kind', sa.Integer(), nullable=False, comment='Type of the experiment'),
    sa.Column('duration', sa.Float(), nullable=True, comment='Duration of the experiment in seconds'),
    sa.Column('status', sa.Integer(), nullable=False, comment='Status of the experiment,             0: UNKNOWN, 1: PENDING, 2: RUNNING, 9: COMPLETED,             10: CANCELLED, 11: FAILED'),
    sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('is_del', sa.Integer(), nullable=True, comment='0 for not deleted, 1 for deleted'),
    sa.PrimaryKeyConstraint('uuid')
    )
    op.create_index('idx_experiment_team_active_time', 'experiments', ['team_id', 'is_del', 'created_at'], unique=False)
    op.create_index('idx_experiment_team_name', 'experiments', ['team_id', 'name', 'is_del'], unique=False)
    op.create_table('metrics',
    sa.Column('uuid', sa.UUID(), nullable=False),
    sa.Column('key', sa.String(), nullable=False),
    sa.Column('value', sa.Float(), nullable=False),
    sa.Column('org_id', sa.UUID(), nullable=False, comment='Organization ID'),
    sa.Column('team_id', sa.UUID(), nullable=False),
    sa.Column('experiment_id', sa.UUID(), nullable=False),
    sa.Column('run_id', sa.UUID(), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
    sa.PrimaryKeyConstraint('uuid'),
    sa.UniqueConstraint('run_id', 'key', name='idx_unique_metric')
    )
    op.create_index('idx_metric_experiment_time', 'metrics', ['experiment_id', 'created_at'], unique=False)
    op.create_index('idx_metric_run_time', 'metrics', ['run_id', 'created_at'], unique=False)
    op.create_table('organizations',
    sa.Column('uuid', sa.UUID(), nullable=False),
    sa.Column('name', sa.String(), nullable=False),
    sa.Column('description', sa.String(), nullable=True),
    sa.Column('meta', sa.JSON(), nullable=True, comment='Additional metadata for the org'),
    sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('is_del', sa.Integer(), nullable=True, comment='0 for not deleted, 1 for deleted'),
    sa.PrimaryKeyConstraint('uuid')
    )
    op.create_table('runs',
    sa.Column('uuid', sa.UUID(), nullable=False),
    sa.Column('org_id', sa.UUID(), nullable=False, comment='Organization ID'),
    sa.Column('team_id', sa.UUID(), nullable=False),
    sa.Column('user_id', sa.UUID(), nullable=False, comment='User who created the run'),
    sa.Column('experiment_id', sa.UUID(), nullable=True, comment='Experiment this run belongs to'),
    sa.Column('session_id', sa.UUID(), nullable=True, comment='Session this run belongs to'),
    sa.Column('meta', sa.JSON(), nullable=True, comment='Additional metadata for the run'),
    sa.Column('duration', sa.Float(), nullable=True, comment='Duration of the run in seconds'),
    sa.Column('status', sa.Integer(), nullable=False, comment='Status of the run,             0: UNKNOWN, 1: PENDING, 2: RUNNING, 9: COMPLETED,             10: CANCELLED, 11: FAILED'),
    sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('is_del', sa.Integer(), nullable=True, comment='0 for not deleted, 1 for deleted'),
    sa.PrimaryKeyConstraint('uuid')
    )
    op.create_index('idx_run_experiment_active', 'runs', ['experiment_id', 'is_del', 'created_at'], unique=False)
    op.create_index('idx_run_session_active', 'runs', ['session_id', 'is_del', 'created_at'], unique=False)
    op.create_index('idx_run_team_active', 'runs', ['team_id', 'is_del'], unique=False)
    op.create_table('sessions',
    sa.Column('uuid', sa.UUID(), nullable=False),
    sa.Column('org_id', sa.UUID(), nullable=False, comment='Organization ID'),
    sa.Column('team_id', sa.UUID(), nullable=False),
    sa.Column('user_id', sa.UUID(), nullable=False, comment='User who created the session'),
    sa.Column('agent_id', sa.UUID(), nullable=False, comment='Agent this session belongs to'),
    sa.Column('meta', sa.JSON(), nullable=True, comment='Additional metadata for the session (e.g. conversation context)'),
    sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('is_del', sa.Integer(), nullable=True, comment='0 for not deleted, 1 for deleted'),
    sa.PrimaryKeyConstraint('uuid')
    )
    op.create_table('team_members',
    sa.Column('uuid', sa.UUID(), nullable=False),
    sa.Column('org_id', sa.UUID(), nullable=False, comment='Organization ID'),
    sa.Column('team_id', sa.UUID(), nullable=False),
    sa.Column('user_id', sa.UUID(), nullable=False),
    sa.Column('role', sa.Integer(), nullable=False, comment='Role of the member in the team'),
    sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
    sa.PrimaryKeyConstraint('uuid'),
    sa.UniqueConstraint('team_id', 'user_id', name='unique_team_user')
    )
    op.create_index(op.f('ix_team_members_user_id'), 'team_members', ['user_id'], unique=False)
    op.create_table('teams',
    sa.Column('uuid', sa.UUID(), nullable=False),
    sa.Column('org_id', sa.UUID(), nullable=False, comment='Organization ID'),
    sa.Column('name', sa.String(), nullable=False),
    sa.Column('description', sa.String(), nullable=True),
    sa.Column('meta', sa.JSON(), nullable=True, comment='Additional metadata for the team'),
    sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('is_del', sa.Integer(), nullable=True, comment='0 for not deleted, 1 for deleted'),
    sa.PrimaryKeyConstraint('uuid')
    )
    op.create_table('users',
    sa.Column('uuid', sa.UUID(), nullable=False),
    sa.Column('org_id', sa.UUID(), nullable=False, comment='Organization ID'),
    sa.Column('name', sa.String(), nullable=False),
    sa.Column('email', sa.String(), nullable=False),
    sa.Column('password_hash', sa.String(), nullable=False, comment='Bcrypt hashed password'),
    sa.Column('avatar_url', sa.String(), nullable=True),
    sa.Column('meta', sa.JSON(), nullable=True, comment='Additional metadata for the user'),
    sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('is_del', sa.Integer(), nullable=True, comment='0 for not deleted, 1 for deleted'),
    sa.PrimaryKeyConstraint('uuid'),
    sa.UniqueConstraint('email')
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('users')
    op.drop_table('teams')
    op.drop_index(op.f('ix_team_members_user_id'), table_name='team_members')
    op.drop_table('team_members')
    op.drop_table('sessions')
    op.drop_index('idx_run_team_active', table_name='runs')
    op.drop_index('idx_run_session_active', table_name='runs')
    op.drop_index('idx_run_experiment_active', table_name='runs')
    op.drop_table('runs')
    op.drop_table('organizations')
    op.drop_index('idx_metric_run_time', table_name='metrics')
    op.drop_index('idx_metric_experiment_time', table_name='metrics')
    op.drop_table('metrics')
    op.drop_index('idx_experiment_team_name', table_name='experiments')
    op.drop_index('idx_experiment_team_active_time', table_name='experiments')
    op.drop_table('experiments')
    op.drop_index('idx_experiment_tag_team', table_name='experiment_tags')
    op.drop_index('idx_experiment_tag_lookup', table_name='experiment_tags')
    op.drop_table('experiment_tags')
    op.drop_index('idx_experiment_label_lookup', table_name='experiment_labels')
    op.drop_table('experiment_labels')
    op.drop_table('datasets')
    op.drop_table('agents')
    # ### end Alembic commands ###
