"""
Notification models for email and SMS notifications.
"""
import enum
from datetime import datetime
from sqlalchemy import Column, Integer, String, Text, DateTime, Boolean, Enum, ForeignKey
from sqlalchemy.orm import relationship

from app.database import Base


class NotificationType(str, enum.Enum):
    """Types of notifications."""
    EMAIL = "EMAIL"
    SMS = "SMS"


class NotificationTemplate(str, enum.Enum):
    """Notification templates."""
    RESERVATION_CONFIRMATION = "RESERVATION_CONFIRMATION"
    RESERVATION_MODIFICATION = "RESERVATION_MODIFICATION"
    RESERVATION_CANCELLATION = "RESERVATION_CANCELLATION"
    RESERVATION_REMINDER = "RESERVATION_REMINDER"
    CHECK_IN_CONFIRMATION = "CHECK_IN_CONFIRMATION"
    CHECK_OUT_RECEIPT = "CHECK_OUT_RECEIPT"
    PAYMENT_RECEIPT = "PAYMENT_RECEIPT"
    INVOICE = "INVOICE"


class NotificationStatus(str, enum.Enum):
    """Status of notification sending."""
    PENDING = "PENDING"
    SENT = "SENT"
    FAILED = "FAILED"
    CANCELLED = "CANCELLED"


class Notification(Base):
    """
    Notification log - tracks all notifications sent to clients.
    """
    __tablename__ = "notifications"

    id = Column(Integer, primary_key=True, index=True)

    # Type and template
    notification_type = Column(Enum(NotificationType), nullable=False, index=True)
    template = Column(Enum(NotificationTemplate), nullable=False, index=True)

    # Recipient
    recipient_email = Column(String(255), index=True)
    recipient_phone = Column(String(50), index=True)
    recipient_name = Column(String(255))

    # Content
    subject = Column(String(500))
    body = Column(Text, nullable=False)

    # Status tracking
    status = Column(Enum(NotificationStatus), default=NotificationStatus.PENDING, index=True)
    sent_at = Column(DateTime, nullable=True)
    error_message = Column(Text, nullable=True)
    retry_count = Column(Integer, default=0)

    # Related entities
    client_id = Column(Integer, ForeignKey("clients.id", ondelete="SET NULL"), nullable=True, index=True)
    reservation_id = Column(Integer, ForeignKey("reservations.id", ondelete="SET NULL"), nullable=True, index=True)
    folio_id = Column(Integer, ForeignKey("folios.id", ondelete="SET NULL"), nullable=True, index=True)

    # Metadata
    created_at = Column(DateTime, default=datetime.utcnow, index=True)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

    # Relationships
    client = relationship("Client", back_populates="notifications")
    reservation = relationship("Reservation", back_populates="notifications")
    folio = relationship("Folio", back_populates="notifications")


class NotificationSetting(Base):
    """
    Email/SMS configuration settings per establishment.
    """
    __tablename__ = "notification_settings"

    id = Column(Integer, primary_key=True, index=True)
    establishment_id = Column(Integer, ForeignKey("establishments.id", ondelete="CASCADE"), nullable=False, unique=True, index=True)

    # Email settings
    smtp_host = Column(String(255))
    smtp_port = Column(Integer, default=587)
    smtp_username = Column(String(255))
    smtp_password = Column(String(255))  # Should be encrypted in production
    smtp_use_tls = Column(Boolean, default=True)
    smtp_use_ssl = Column(Boolean, default=False)
    from_email = Column(String(255))
    from_name = Column(String(255))

    # SMS settings (for future implementation)
    sms_provider = Column(String(50))  # twilio, aws_sns, etc.
    sms_api_key = Column(String(255))
    sms_api_secret = Column(String(255))
    sms_from_number = Column(String(50))

    # Feature flags
    email_enabled = Column(Boolean, default=True)
    sms_enabled = Column(Boolean, default=False)

    # Auto-send settings
    auto_send_confirmation = Column(Boolean, default=True)
    auto_send_modification = Column(Boolean, default=True)
    auto_send_cancellation = Column(Boolean, default=True)
    auto_send_reminder = Column(Boolean, default=True)
    reminder_days_before = Column(Integer, default=1)  # Send reminder X days before check-in

    # Metadata
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

    # Relationships
    establishment = relationship("Establishment", back_populates="notification_settings")
