"""initial sprint 2 tables

Revision ID: 2ce3bb57b0c5
Revises: 
Create Date: 2026-03-14 17:30:11.464514+00:00
"""

from __future__ import annotations

from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op


# revision identifiers, used by Alembic.
revision: str = '2ce3bb57b0c5'
down_revision: Union[str, None] = None
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('agent_runs',
    sa.Column('id', sa.String(length=26), nullable=False),
    sa.Column('tenant_id', sa.String(length=255), nullable=True),
    sa.Column('agent_name', sa.String(length=255), nullable=False),
    sa.Column('status', sa.String(length=50), nullable=False),
    sa.Column('input_data', sa.JSON(), nullable=True),
    sa.Column('output_data', sa.JSON(), nullable=True),
    sa.Column('iteration_count', sa.Integer(), nullable=False),
    sa.Column('model', sa.String(length=255), nullable=True),
    sa.Column('strategy', sa.String(length=100), nullable=True),
    sa.Column('parent_run_id', sa.String(length=26), nullable=True),
    sa.Column('delegation_level', sa.Integer(), nullable=False),
    sa.Column('total_input_tokens', sa.Integer(), nullable=False),
    sa.Column('total_output_tokens', sa.Integer(), nullable=False),
    sa.Column('total_cost_usd', sa.Numeric(precision=10, scale=6), nullable=True),
    sa.Column('meta', sa.JSON(), nullable=True),
    sa.Column('error', sa.Text(), nullable=True),
    sa.Column('created_at', sa.DateTime(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.Column('updated_at', sa.DateTime(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.ForeignKeyConstraint(['parent_run_id'], ['agent_runs.id'], ondelete='SET NULL'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('ix_agent_runs_created_at', 'agent_runs', ['created_at'], unique=False)
    op.create_index('ix_agent_runs_parent_run_id', 'agent_runs', ['parent_run_id'], unique=False)
    op.create_index('ix_agent_runs_status', 'agent_runs', ['status'], unique=False)
    op.create_index(op.f('ix_agent_runs_tenant_id'), 'agent_runs', ['tenant_id'], unique=False)
    op.create_table('react_traces',
    sa.Column('id', sa.String(length=26), nullable=False),
    sa.Column('agent_run_id', sa.String(length=26), nullable=False),
    sa.Column('role', sa.String(length=20), nullable=False),
    sa.Column('content', sa.Text(), nullable=False),
    sa.Column('order_index', sa.Integer(), nullable=False),
    sa.Column('meta', sa.JSON(), nullable=True),
    sa.Column('created_at', sa.DateTime(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.Column('updated_at', sa.DateTime(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.ForeignKeyConstraint(['agent_run_id'], ['agent_runs.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('ix_react_traces_agent_run_id_order', 'react_traces', ['agent_run_id', 'order_index'], unique=False)
    op.create_table('token_usage',
    sa.Column('id', sa.String(length=26), nullable=False),
    sa.Column('agent_run_id', sa.String(length=26), nullable=False),
    sa.Column('iteration_index', sa.Integer(), nullable=False),
    sa.Column('model', sa.String(length=255), nullable=True),
    sa.Column('provider', sa.String(length=100), nullable=True),
    sa.Column('input_tokens', sa.Integer(), nullable=False),
    sa.Column('output_tokens', sa.Integer(), nullable=False),
    sa.Column('cost_usd', sa.Numeric(precision=10, scale=6), nullable=True),
    sa.Column('duration_ms', sa.Integer(), nullable=True),
    sa.Column('meta', sa.JSON(), nullable=True),
    sa.Column('created_at', sa.DateTime(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.ForeignKeyConstraint(['agent_run_id'], ['agent_runs.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('ix_token_usage_agent_run_id', 'token_usage', ['agent_run_id'], unique=False)
    op.create_table('tool_calls',
    sa.Column('id', sa.String(length=26), nullable=False),
    sa.Column('agent_run_id', sa.String(length=26), nullable=False),
    sa.Column('tool_call_id', sa.String(length=26), nullable=False),
    sa.Column('provider_tool_call_id', sa.String(length=255), nullable=True),
    sa.Column('tool_name', sa.String(length=255), nullable=False),
    sa.Column('target', sa.String(length=50), nullable=False),
    sa.Column('params', sa.JSON(), nullable=True),
    sa.Column('result', sa.JSON(), nullable=True),
    sa.Column('success', sa.Boolean(), nullable=False),
    sa.Column('duration_ms', sa.Integer(), nullable=True),
    sa.Column('iteration_index', sa.Integer(), nullable=True),
    sa.Column('error_message', sa.Text(), nullable=True),
    sa.Column('meta', sa.JSON(), nullable=True),
    sa.Column('created_at', sa.DateTime(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.Column('updated_at', sa.DateTime(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.ForeignKeyConstraint(['agent_run_id'], ['agent_runs.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('ix_tool_calls_agent_run_id', 'tool_calls', ['agent_run_id'], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    raise NotImplementedError(
        "Downgrading the initial migration would destroy all data. "
        "If you really need this, drop the tables manually."
    )
