"""remove_context_system

Revision ID: 20260118_172743
Revises: 20260118_132847
Create Date: 2026-01-18 17:27:43

Removes the entire conversation context/recall system from ClaudeTools.

This migration drops all context-related tables:
- context_tags (normalized tags table)
- project_states (project state tracking)
- decision_logs (decision documentation)
- conversation_contexts (main context storage)
- context_snippets (knowledge fragments)

WARNING: This is a destructive operation. All context data will be lost.
Make sure to export any important data before running this migration.
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


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


def upgrade() -> None:
    """
    Drop all context-related tables.

    Tables are dropped in reverse dependency order to avoid foreign key violations.
    """

    # Step 1: Drop context_tags table (depends on conversation_contexts)
    op.drop_index('idx_context_tags_tag_context', table_name='context_tags')
    op.drop_index('idx_context_tags_context', table_name='context_tags')
    op.drop_index('idx_context_tags_tag', table_name='context_tags')
    op.drop_table('context_tags')

    # Step 2: Drop project_states table (depends on projects, sessions)
    op.drop_index('idx_project_states_project', table_name='project_states')
    op.drop_index('idx_project_states_progress', table_name='project_states')
    op.drop_index('idx_project_states_last_session', table_name='project_states')
    op.drop_table('project_states')

    # Step 3: Drop decision_logs table (depends on projects, sessions)
    op.drop_index('idx_decision_logs_type', table_name='decision_logs')
    op.drop_index('idx_decision_logs_session', table_name='decision_logs')
    op.drop_index('idx_decision_logs_project', table_name='decision_logs')
    op.drop_index('idx_decision_logs_impact', table_name='decision_logs')
    op.drop_table('decision_logs')

    # Step 4: Drop conversation_contexts table (depends on sessions, projects, machines)
    op.drop_index('idx_conversation_contexts_type', table_name='conversation_contexts')
    op.drop_index('idx_conversation_contexts_session', table_name='conversation_contexts')
    op.drop_index('idx_conversation_contexts_relevance', table_name='conversation_contexts')
    op.drop_index('idx_conversation_contexts_project', table_name='conversation_contexts')
    op.drop_index('idx_conversation_contexts_machine', table_name='conversation_contexts')
    op.drop_table('conversation_contexts')

    # Step 5: Drop context_snippets table (depends on projects, clients)
    op.drop_index('idx_context_snippets_usage', table_name='context_snippets')
    op.drop_index('idx_context_snippets_relevance', table_name='context_snippets')
    op.drop_index('idx_context_snippets_project', table_name='context_snippets')
    op.drop_index('idx_context_snippets_client', table_name='context_snippets')
    op.drop_index('idx_context_snippets_category', table_name='context_snippets')
    op.drop_table('context_snippets')


def downgrade() -> None:
    """
    Recreating the context system is not supported.

    This is a one-way migration. If you need to restore the context system,
    you should restore from a database backup or re-run the original migrations.
    """
    raise NotImplementedError(
        "Downgrade not supported. Restore from backup or re-run original migrations."
    )
