"""
SQLAlchemy models for events table.
"""

from sqlalchemy import Column, String, Integer, DateTime, Float, Index
from sqlalchemy.dialects.postgresql import UUID, JSONB
from sqlalchemy.ext.declarative import declarative_base
from datetime import datetime, timezone
import uuid

Base = declarative_base()


class Event(Base):
    """Unified event table for alerts, notifications, recommendations"""
    __tablename__ = "events"

    # Core fields
    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    tenant_id = Column(UUID(as_uuid=True), nullable=False, index=True)
    created_at = Column(
        DateTime(timezone=True),
        default=lambda: datetime.now(timezone.utc),
        nullable=False
    )
    updated_at = Column(
        DateTime(timezone=True),
        default=lambda: datetime.now(timezone.utc),
        onupdate=lambda: datetime.now(timezone.utc),
        nullable=False
    )

    # Classification
    event_class = Column(String(50), nullable=False)
    event_domain = Column(String(50), nullable=False, index=True)
    event_type = Column(String(100), nullable=False, index=True)
    service = Column(String(50), nullable=False)

    # i18n content (NO hardcoded title/message)
    i18n_title_key = Column(String(200), nullable=False)
    i18n_title_params = Column(JSONB, nullable=False, default=dict)
    i18n_message_key = Column(String(200), nullable=False)
    i18n_message_params = Column(JSONB, nullable=False, default=dict)

    # Priority
    priority_score = Column(Integer, nullable=False, default=50, index=True)
    priority_level = Column(String(20), nullable=False, index=True)
    type_class = Column(String(50), nullable=False, index=True)

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

    # Smart actions
    smart_actions = Column(JSONB, nullable=False, default=list)

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

    # Entity references
    entity_links = Column(JSONB, nullable=False, default=dict)

    # Status
    status = Column(String(20), nullable=False, default="active", index=True)
    resolved_at = Column(DateTime(timezone=True), nullable=True)
    acknowledged_at = Column(DateTime(timezone=True), nullable=True)

    # Metadata
    event_metadata = Column(JSONB, nullable=False, default=dict)

    # Indexes for dashboard queries
    __table_args__ = (
        Index('idx_events_tenant_status', 'tenant_id', 'status'),
        Index('idx_events_tenant_priority', 'tenant_id', 'priority_score'),
        Index('idx_events_tenant_class', 'tenant_id', 'event_class'),
        Index('idx_events_tenant_created', 'tenant_id', 'created_at'),
        Index('idx_events_type_class_status', 'type_class', 'status'),
    )
