# services/alert_processor/app/models/alerts.py
"""
Alert models for the alert processor service
"""

from sqlalchemy import Column, String, Text, DateTime, JSON, Enum, Integer, ForeignKey
from sqlalchemy.dialects.postgresql import UUID, JSONB
from datetime import datetime, timezone
import uuid
import enum

from shared.database.base import Base


def utc_now():
    """Return current UTC time as timezone-aware datetime"""
    return datetime.now(timezone.utc)


class AlertStatus(enum.Enum):
    """Alert status values"""
    ACTIVE = "active"
    RESOLVED = "resolved"
    ACKNOWLEDGED = "acknowledged"
    IGNORED = "ignored"


class AlertSeverity(enum.Enum):
    """Alert severity levels"""
    LOW = "low"
    MEDIUM = "medium"
    HIGH = "high"
    URGENT = "urgent"


class InteractionType(enum.Enum):
    """Alert interaction types"""
    ACKNOWLEDGED = "acknowledged"
    RESOLVED = "resolved"
    SNOOZED = "snoozed"
    DISMISSED = "dismissed"


class Alert(Base):
    """Alert records for the alert processor service"""
    __tablename__ = "alerts"

    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    tenant_id = Column(UUID(as_uuid=True), nullable=False, index=True)

    # Alert classification
    item_type = Column(String(50), nullable=False)  # 'alert' or 'recommendation'
    alert_type = Column(String(100), nullable=False)  # e.g., 'overstock_warning'
    severity = Column(Enum(AlertSeverity, values_callable=lambda obj: [e.value for e in obj]), nullable=False, index=True)
    status = Column(Enum(AlertStatus, values_callable=lambda obj: [e.value for e in obj]), default=AlertStatus.ACTIVE, index=True)

    # Source and content
    service = Column(String(100), nullable=False)  # originating service
    title = Column(String(255), nullable=False)
    message = Column(Text, nullable=False)

    # Actions and metadata
    actions = Column(JSON, nullable=True)  # List of available actions
    alert_metadata = Column(JSON, nullable=True)  # Additional alert-specific data

    # Timestamps
    created_at = Column(DateTime(timezone=True), default=utc_now, index=True)
    updated_at = Column(DateTime(timezone=True), default=utc_now, onupdate=utc_now)
    resolved_at = Column(DateTime(timezone=True), nullable=True)


class AlertInteraction(Base):
    """Alert interaction tracking for analytics"""
    __tablename__ = "alert_interactions"

    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    tenant_id = Column(UUID(as_uuid=True), nullable=False, index=True)
    alert_id = Column(UUID(as_uuid=True), ForeignKey('alerts.id', ondelete='CASCADE'), nullable=False)
    user_id = Column(UUID(as_uuid=True), nullable=False, index=True)

    # Interaction details
    interaction_type = Column(String(50), nullable=False, index=True)
    interacted_at = Column(DateTime(timezone=True), nullable=False, default=utc_now, index=True)
    response_time_seconds = Column(Integer, nullable=True)

    # Context
    interaction_metadata = Column(JSONB, nullable=True)

    # Timestamps
    created_at = Column(DateTime(timezone=True), nullable=False, default=utc_now)