"""
Clean unified events table schema.

Revision ID: 20251205_unified
Revises:
Create Date: 2025-12-05
"""

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

# revision identifiers
revision = '20251205_unified'
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
    """
    Create unified events table with JSONB enrichment contexts.
    """

    # Create events table
    op.create_table(
        'events',

        # Core fields
        sa.Column('id', postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column('tenant_id', postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
        sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False),

        # Classification
        sa.Column('event_class', sa.String(50), nullable=False),
        sa.Column('event_domain', sa.String(50), nullable=False),
        sa.Column('event_type', sa.String(100), nullable=False),
        sa.Column('service', sa.String(50), nullable=False),

        # i18n content (NO hardcoded title/message)
        sa.Column('i18n_title_key', sa.String(200), nullable=False),
        sa.Column('i18n_title_params', postgresql.JSONB, nullable=False, server_default=sa.text("'{}'::jsonb")),
        sa.Column('i18n_message_key', sa.String(200), nullable=False),
        sa.Column('i18n_message_params', postgresql.JSONB, nullable=False, server_default=sa.text("'{}'::jsonb")),

        # Priority
        sa.Column('priority_score', sa.Integer, nullable=False, server_default='50'),
        sa.Column('priority_level', sa.String(20), nullable=False),
        sa.Column('type_class', sa.String(50), nullable=False),

        # Enrichment contexts (JSONB)
        sa.Column('orchestrator_context', postgresql.JSONB, nullable=True),
        sa.Column('business_impact', postgresql.JSONB, nullable=True),
        sa.Column('urgency', postgresql.JSONB, nullable=True),
        sa.Column('user_agency', postgresql.JSONB, nullable=True),
        sa.Column('trend_context', postgresql.JSONB, nullable=True),

        # Smart actions
        sa.Column('smart_actions', postgresql.JSONB, nullable=False, server_default=sa.text("'[]'::jsonb")),

        # AI reasoning
        sa.Column('ai_reasoning_summary_key', sa.String(200), nullable=True),
        sa.Column('ai_reasoning_summary_params', postgresql.JSONB, nullable=True),
        sa.Column('ai_reasoning_details', postgresql.JSONB, nullable=True),
        sa.Column('confidence_score', sa.Float, nullable=True),

        # Entity references
        sa.Column('entity_links', postgresql.JSONB, nullable=False, server_default=sa.text("'{}'::jsonb")),

        # Status
        sa.Column('status', sa.String(20), nullable=False, server_default='active'),
        sa.Column('resolved_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('acknowledged_at', sa.DateTime(timezone=True), nullable=True),

        # Metadata
        sa.Column('event_metadata', postgresql.JSONB, nullable=False, server_default=sa.text("'{}'::jsonb"))
    )

    # Create indexes for efficient queries (matching SQLAlchemy model)
    op.create_index('idx_events_tenant_status', 'events', ['tenant_id', 'status'])
    op.create_index('idx_events_tenant_priority', 'events', ['tenant_id', 'priority_score'])
    op.create_index('idx_events_tenant_class', 'events', ['tenant_id', 'event_class'])
    op.create_index('idx_events_tenant_created', 'events', ['tenant_id', 'created_at'])
    op.create_index('idx_events_type_class_status', 'events', ['type_class', 'status'])


def downgrade():
    """
    Drop events table and all indexes.
    """
    op.drop_index('idx_events_type_class_status', 'events')
    op.drop_index('idx_events_tenant_created', 'events')
    op.drop_index('idx_events_tenant_class', 'events')
    op.drop_index('idx_events_tenant_priority', 'events')
    op.drop_index('idx_events_tenant_status', 'events')
    op.drop_table('events')
