# ================================================================
# services/notification/app/models/templates.py
# ================================================================
"""
Template-specific models for email and WhatsApp templates
"""

from sqlalchemy import Column, String, Text, Boolean, DateTime, JSON, Integer
from sqlalchemy.dialects.postgresql import UUID
from datetime import datetime
import uuid

from shared.database.base import Base

class EmailTemplate(Base):
    """Email-specific templates with HTML support"""
    __tablename__ = "email_templates"

    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    tenant_id = Column(UUID(as_uuid=True), nullable=True, index=True)
    
    # Template identification
    template_key = Column(String(100), nullable=False, unique=True)
    name = Column(String(255), nullable=False)
    description = Column(Text, nullable=True)
    
    # Email-specific content
    subject = Column(String(255), nullable=False)
    html_body = Column(Text, nullable=False)
    text_body = Column(Text, nullable=True)  # Plain text fallback
    
    # Email settings
    from_email = Column(String(255), nullable=True)
    from_name = Column(String(255), nullable=True)
    reply_to = Column(String(255), nullable=True)
    
    # Template variables
    variables = Column(JSON, nullable=True)  # Expected variables and their types
    sample_data = Column(JSON, nullable=True)  # Sample data for preview
    
    # Configuration
    language = Column(String(2), default="es")
    is_active = Column(Boolean, default=True)
    is_system = Column(Boolean, default=False)
    
    # Timestamps
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)


# NOTE: WhatsAppTemplate has been moved to app/models/whatsapp_messages.py
# This old definition is commented out to avoid duplicate table definition errors
# class WhatsAppTemplate(Base):
#     """WhatsApp-specific templates"""
#     __tablename__ = "whatsapp_templates"
#
#     id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
#     tenant_id = Column(UUID(as_uuid=True), nullable=True, index=True)
#
#     # Template identification
#     template_key = Column(String(100), nullable=False, unique=True)
#     name = Column(String(255), nullable=False)
#
#     # WhatsApp template details
#     whatsapp_template_name = Column(String(255), nullable=False)  # Template name in WhatsApp Business API
#     whatsapp_template_id = Column(String(255), nullable=True)
#     language_code = Column(String(10), default="es")
#
#     # Template content
#     header_text = Column(String(60), nullable=True)  # WhatsApp header limit
#     body_text = Column(Text, nullable=False)
#     footer_text = Column(String(60), nullable=True)  # WhatsApp footer limit
#
#     # Template parameters
#     parameter_count = Column(Integer, default=0)
#     parameters = Column(JSON, nullable=True)  # Parameter definitions
#
#     # Status
#     approval_status = Column(String(20), default="pending")  # pending, approved, rejected
#     is_active = Column(Boolean, default=True)
#
#     # Timestamps
#     created_at = Column(DateTime, default=datetime.utcnow)
#     updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)