"""initial_schema

Revision ID: d1cf5ca56f2b
Revises: 
Create Date: 2026-04-05 13:52:21.755428

"""
from typing import Sequence, Union

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

# revision identifiers, used by Alembic.
revision: str = 'd1cf5ca56f2b'
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.drop_table('loop_states')
    op.alter_column('agent_activities', 'status',
               existing_type=sa.VARCHAR(length=20),
               nullable=False,
               existing_server_default=sa.text('("completed")'))
    op.alter_column('agent_activities', 'tokens_input',
               existing_type=sa.INTEGER(),
               nullable=False,
               existing_server_default=sa.text('0'))
    op.alter_column('agent_activities', 'tokens_output',
               existing_type=sa.INTEGER(),
               nullable=False,
               existing_server_default=sa.text('0'))
    op.alter_column('agent_activities', 'cost_usd',
               existing_type=sa.REAL(),
               type_=sa.Float(),
               nullable=False,
               existing_server_default=sa.text('(0.0)'))
    op.drop_index(op.f('idx_activities_agent'), table_name='agent_activities')
    op.drop_index(op.f('idx_activities_session'), table_name='agent_activities')
    op.alter_column('agents', 'source',
               existing_type=sa.TEXT(),
               type_=sa.String(length=20),
               nullable=False,
               existing_server_default=sa.text('("api")'))
    op.alter_column('agents', 'session_id',
               existing_type=sa.TEXT(),
               type_=sa.String(length=100),
               existing_nullable=True)
    op.alter_column('agents', 'cc_tool_use_id',
               existing_type=sa.TEXT(),
               type_=sa.String(length=100),
               existing_nullable=True)
    op.create_index(op.f('ix_events_entity_id'), 'events', ['entity_id'], unique=False)
    op.alter_column('tasks', 'id',
               existing_type=sa.VARCHAR(length=36),
               nullable=False)
    op.alter_column('tasks', 'description',
               existing_type=sa.TEXT(),
               nullable=False,
               existing_server_default=sa.text("('')"))
    op.alter_column('tasks', 'status',
               existing_type=sa.VARCHAR(length=20),
               nullable=False,
               existing_server_default=sa.text("'pending'"))
    op.alter_column('tasks', 'depends_on',
               existing_type=sqlite.JSON(),
               nullable=False,
               existing_server_default=sa.text("'[]'"))
    op.alter_column('tasks', 'depth',
               existing_type=sa.INTEGER(),
               nullable=False,
               existing_server_default=sa.text('0'))
    op.alter_column('tasks', 'order',
               existing_type=sa.INTEGER(),
               nullable=False,
               existing_server_default=sa.text('0'))
    op.alter_column('tasks', 'priority',
               existing_type=sa.VARCHAR(length=20),
               nullable=False,
               existing_server_default=sa.text("'medium'"))
    op.alter_column('tasks', 'horizon',
               existing_type=sa.VARCHAR(length=20),
               nullable=False,
               existing_server_default=sa.text("'short'"))
    op.alter_column('tasks', 'tags',
               existing_type=sqlite.JSON(),
               nullable=False,
               existing_server_default=sa.text("'[]'"))
    op.alter_column('tasks', 'config',
               existing_type=sqlite.JSON(),
               nullable=False,
               existing_server_default=sa.text("'{}'"))
    op.alter_column('tasks', 'created_at',
               existing_type=sa.DATETIME(),
               nullable=False)
    op.alter_column('teams', 'status',
               existing_type=sa.VARCHAR(length=20),
               nullable=False,
               existing_server_default=sa.text("'active'"))
    op.alter_column('teams', 'summary',
               existing_type=sa.VARCHAR(length=500),
               nullable=False,
               existing_server_default=sa.text('("")'))
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column('teams', 'summary',
               existing_type=sa.VARCHAR(length=500),
               nullable=True,
               existing_server_default=sa.text('("")'))
    op.alter_column('teams', 'status',
               existing_type=sa.VARCHAR(length=20),
               nullable=True,
               existing_server_default=sa.text("'active'"))
    op.alter_column('tasks', 'created_at',
               existing_type=sa.DATETIME(),
               nullable=True)
    op.alter_column('tasks', 'config',
               existing_type=sqlite.JSON(),
               nullable=True,
               existing_server_default=sa.text("'{}'"))
    op.alter_column('tasks', 'tags',
               existing_type=sqlite.JSON(),
               nullable=True,
               existing_server_default=sa.text("'[]'"))
    op.alter_column('tasks', 'horizon',
               existing_type=sa.VARCHAR(length=20),
               nullable=True,
               existing_server_default=sa.text("'short'"))
    op.alter_column('tasks', 'priority',
               existing_type=sa.VARCHAR(length=20),
               nullable=True,
               existing_server_default=sa.text("'medium'"))
    op.alter_column('tasks', 'order',
               existing_type=sa.INTEGER(),
               nullable=True,
               existing_server_default=sa.text('0'))
    op.alter_column('tasks', 'depth',
               existing_type=sa.INTEGER(),
               nullable=True,
               existing_server_default=sa.text('0'))
    op.alter_column('tasks', 'depends_on',
               existing_type=sqlite.JSON(),
               nullable=True,
               existing_server_default=sa.text("'[]'"))
    op.alter_column('tasks', 'status',
               existing_type=sa.VARCHAR(length=20),
               nullable=True,
               existing_server_default=sa.text("'pending'"))
    op.alter_column('tasks', 'description',
               existing_type=sa.TEXT(),
               nullable=True,
               existing_server_default=sa.text("('')"))
    op.alter_column('tasks', 'id',
               existing_type=sa.VARCHAR(length=36),
               nullable=True)
    op.drop_index(op.f('ix_events_entity_id'), table_name='events')
    op.alter_column('agents', 'cc_tool_use_id',
               existing_type=sa.String(length=100),
               type_=sa.TEXT(),
               existing_nullable=True)
    op.alter_column('agents', 'session_id',
               existing_type=sa.String(length=100),
               type_=sa.TEXT(),
               existing_nullable=True)
    op.alter_column('agents', 'source',
               existing_type=sa.String(length=20),
               type_=sa.TEXT(),
               nullable=True,
               existing_server_default=sa.text('("api")'))
    op.create_index(op.f('idx_activities_session'), 'agent_activities', ['session_id'], unique=False)
    op.create_index(op.f('idx_activities_agent'), 'agent_activities', ['agent_id'], unique=False)
    op.alter_column('agent_activities', 'cost_usd',
               existing_type=sa.Float(),
               type_=sa.REAL(),
               nullable=True,
               existing_server_default=sa.text('(0.0)'))
    op.alter_column('agent_activities', 'tokens_output',
               existing_type=sa.INTEGER(),
               nullable=True,
               existing_server_default=sa.text('0'))
    op.alter_column('agent_activities', 'tokens_input',
               existing_type=sa.INTEGER(),
               nullable=True,
               existing_server_default=sa.text('0'))
    op.alter_column('agent_activities', 'status',
               existing_type=sa.VARCHAR(length=20),
               nullable=True,
               existing_server_default=sa.text('("completed")'))
    op.create_table('loop_states',
    sa.Column('team_id', sa.VARCHAR(length=36), nullable=True),
    sa.Column('phase', sa.VARCHAR(length=20), server_default=sa.text("'idle'"), nullable=True),
    sa.Column('prev_phase', sa.VARCHAR(length=20), nullable=True),
    sa.Column('current_cycle', sa.INTEGER(), server_default=sa.text('0'), nullable=True),
    sa.Column('completed_tasks_count', sa.INTEGER(), server_default=sa.text('0'), nullable=True),
    sa.Column('current_task_id', sa.VARCHAR(length=36), nullable=True),
    sa.Column('review_interval', sa.INTEGER(), server_default=sa.text('5'), nullable=True),
    sa.Column('updated_at', sa.DATETIME(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
    sa.PrimaryKeyConstraint('team_id')
    )
    # ### end Alembic commands ###
