"""
Email Type Enum

Defines all system email types that can have customizable templates.
Each business (tenant) has one template per email type.
"""
from enum import Enum


class EmailType(str, Enum):
    """
    Enumeration of all email types in the system.

    Email types are grouped by category:
    - Welcome/Onboarding
    - Appointment-related
    - Contract-related
    - Payment-related
    - Ticket/Support-related
    """

    # ==========================================================================
    # Welcome / Onboarding
    # ==========================================================================
    WELCOME = 'welcome'
    """Sent to new customers after registration or first booking."""

    # ==========================================================================
    # Appointment Lifecycle
    # ==========================================================================
    APPOINTMENT_CONFIRMATION = 'appointment_confirmation'
    """Sent immediately after booking is confirmed."""

    APPOINTMENT_REMINDER = 'appointment_reminder'
    """Sent before appointment (24h, 1h, etc.)."""

    APPOINTMENT_RESCHEDULED = 'appointment_rescheduled'
    """Sent when appointment time is changed."""

    APPOINTMENT_CANCELLED = 'appointment_cancelled'
    """Sent when appointment is cancelled."""

    THANK_YOU = 'thank_you'
    """Sent after appointment is completed (follow-up)."""

    # ==========================================================================
    # Contract / E-Signature
    # ==========================================================================
    CONTRACT_SIGNING_REQUEST = 'contract_signing_request'
    """Sent when a contract is created and needs signature."""

    CONTRACT_REMINDER = 'contract_reminder'
    """Reminder to sign pending contract."""

    CONTRACT_SIGNED = 'contract_signed'
    """Confirmation that contract was successfully signed."""

    # ==========================================================================
    # Payment
    # ==========================================================================
    PAYMENT_RECEIPT = 'payment_receipt'
    """Receipt sent after successful payment."""

    INVOICE = 'invoice'
    """Invoice sent for payment due."""

    PAYMENT_REMINDER = 'payment_reminder'
    """Reminder for unpaid invoice."""

    # ==========================================================================
    # Support Tickets
    # ==========================================================================
    TICKET_ASSIGNED = 'ticket_assigned'
    """Sent to staff when ticket is assigned to them."""

    TICKET_REPLY = 'ticket_reply'
    """Sent when someone replies to a ticket."""

    TICKET_RESOLVED = 'ticket_resolved'
    """Sent when ticket is marked as resolved."""

    # ==========================================================================
    # Utility Methods
    # ==========================================================================

    @classmethod
    def choices(cls):
        """Return choices tuple for Django model field."""
        return [(e.value, e.value.replace('_', ' ').title()) for e in cls]

    @classmethod
    def get_category(cls, email_type: 'EmailType') -> str:
        """Get the category for an email type."""
        categories = {
            # Welcome
            cls.WELCOME: 'welcome',
            # Appointment
            cls.APPOINTMENT_CONFIRMATION: 'appointment',
            cls.APPOINTMENT_REMINDER: 'appointment',
            cls.APPOINTMENT_RESCHEDULED: 'appointment',
            cls.APPOINTMENT_CANCELLED: 'appointment',
            cls.THANK_YOU: 'appointment',
            # Contract
            cls.CONTRACT_SIGNING_REQUEST: 'contract',
            cls.CONTRACT_REMINDER: 'contract',
            cls.CONTRACT_SIGNED: 'contract',
            # Payment
            cls.PAYMENT_RECEIPT: 'payment',
            cls.INVOICE: 'payment',
            cls.PAYMENT_REMINDER: 'payment',
            # Ticket
            cls.TICKET_ASSIGNED: 'ticket',
            cls.TICKET_REPLY: 'ticket',
            cls.TICKET_RESOLVED: 'ticket',
        }
        return categories.get(email_type, 'other')

    @classmethod
    def get_display_name(cls, email_type: 'EmailType') -> str:
        """Get human-readable display name for email type."""
        display_names = {
            cls.WELCOME: 'Welcome Email',
            cls.APPOINTMENT_CONFIRMATION: 'Appointment Confirmation',
            cls.APPOINTMENT_REMINDER: 'Appointment Reminder',
            cls.APPOINTMENT_RESCHEDULED: 'Appointment Rescheduled',
            cls.APPOINTMENT_CANCELLED: 'Appointment Cancelled',
            cls.THANK_YOU: 'Thank You / Follow-up',
            cls.CONTRACT_SIGNING_REQUEST: 'Contract Signing Request',
            cls.CONTRACT_REMINDER: 'Contract Reminder',
            cls.CONTRACT_SIGNED: 'Contract Signed Confirmation',
            cls.PAYMENT_RECEIPT: 'Payment Receipt',
            cls.INVOICE: 'Invoice',
            cls.PAYMENT_REMINDER: 'Payment Reminder',
            cls.TICKET_ASSIGNED: 'Ticket Assigned',
            cls.TICKET_REPLY: 'Ticket Reply',
            cls.TICKET_RESOLVED: 'Ticket Resolved',
        }
        return display_names.get(email_type, email_type.value.replace('_', ' ').title())

    @classmethod
    def get_description(cls, email_type: 'EmailType') -> str:
        """Get description of when this email is sent."""
        descriptions = {
            cls.WELCOME: 'Sent to new customers after registration or first booking',
            cls.APPOINTMENT_CONFIRMATION: 'Sent immediately after an appointment is confirmed',
            cls.APPOINTMENT_REMINDER: 'Sent before the appointment (e.g., 24 hours, 1 hour)',
            cls.APPOINTMENT_RESCHEDULED: 'Sent when appointment date/time is changed',
            cls.APPOINTMENT_CANCELLED: 'Sent when an appointment is cancelled',
            cls.THANK_YOU: 'Sent after appointment completion as a follow-up',
            cls.CONTRACT_SIGNING_REQUEST: 'Sent when a contract requires customer signature',
            cls.CONTRACT_REMINDER: 'Reminder to sign a pending contract',
            cls.CONTRACT_SIGNED: 'Confirmation that the contract was signed',
            cls.PAYMENT_RECEIPT: 'Receipt sent after successful payment',
            cls.INVOICE: 'Invoice sent for services requiring payment',
            cls.PAYMENT_REMINDER: 'Reminder for unpaid invoices',
            cls.TICKET_ASSIGNED: 'Notifies staff when a support ticket is assigned',
            cls.TICKET_REPLY: 'Sent when someone replies to a support ticket',
            cls.TICKET_RESOLVED: 'Sent when a support ticket is resolved',
        }
        return descriptions.get(email_type, '')


# =============================================================================
# EMAIL_TYPE_INFO Dictionary
# =============================================================================
# Pre-built dictionary with all email type information for easy consumption
# by external modules like safe_scripting.

EMAIL_TYPE_INFO = {
    email_type: {
        'display_name': EmailType.get_display_name(email_type),
        'description': EmailType.get_description(email_type),
        'category': EmailType.get_category(email_type),
    }
    for email_type in EmailType
}
