"""
Notification Log Model
"""
from enum import Enum
from sqlalchemy import (
    Column, Integer, String, DateTime, Text,
    ForeignKey, Enum as SQLEnum, Index
)
from sqlalchemy.orm import relationship
from app.models.base import Base


class NotificationType(str, Enum):
    SLA_BREACH = "sla_breach"
    ASSIGNMENT = "assignment"
    NEW_VULNERABILITY = "new_vulnerability"
    MANUAL = "manual"


class NotificationStatus(str, Enum):
    SENT = "sent"
    FAILED = "failed"
    SUPPRESSED = "suppressed"


class NotificationLog(Base):
    __tablename__ = "notification_logs"

    id = Column(Integer, primary_key=True, index=True)
    vulnerability_id = Column(Integer, ForeignKey("vulnerabilities.id"), nullable=True, index=True)
    asset_id = Column(Integer, ForeignKey("assets.id"), nullable=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=True, index=True)
    notification_type = Column(SQLEnum(NotificationType), nullable=False, index=True)
    sent_at = Column(DateTime, nullable=False, index=True)
    subject = Column(String(500), nullable=True)
    recipient_email = Column(String(255), nullable=True)
    status = Column(SQLEnum(NotificationStatus), nullable=False)
    message_body = Column(Text, nullable=True)
    error_message = Column(Text, nullable=True)

    # Relationships
    vulnerability = relationship("Vulnerability", foreign_keys=[vulnerability_id])
    asset = relationship("Asset", foreign_keys=[asset_id])
    user = relationship("User", foreign_keys=[user_id])

    def __repr__(self):
        return f"<NotificationLog(type='{self.notification_type}', status='{self.status}')>"


Index(
    'idx_notif_vuln_type_sent',
    NotificationLog.vulnerability_id,
    NotificationLog.notification_type,
    NotificationLog.sent_at
)
