"""Communication domain enums and dialect-safe GUID type."""
from __future__ import annotations

import enum
import uuid

from sqlalchemy.dialects.postgresql import UUID as PG_UUID
from sqlalchemy.types import CHAR, TypeDecorator


class GUID(TypeDecorator):
    impl = CHAR
    cache_ok = True

    def load_dialect_impl(self, dialect):
        if dialect.name == "postgresql":
            return dialect.type_descriptor(PG_UUID(as_uuid=True))
        return dialect.type_descriptor(CHAR(36))

    def process_bind_param(self, value, dialect):
        if value is None:
            return value
        if dialect.name == "postgresql":
            return value if isinstance(value, uuid.UUID) else uuid.UUID(str(value))
        return str(value)

    def process_result_value(self, value, dialect):
        if value is None:
            return value
        if isinstance(value, uuid.UUID):
            return value
        return uuid.UUID(str(value))


class ChannelType(str, enum.Enum):
    SMS = "sms"
    EMAIL = "email"
    PUSH = "push"
    WHATSAPP = "whatsapp"
    TELEGRAM = "telegram"
    RUBIKA = "rubika"
    VOICE = "voice"
    FUTURE = "future"


class ProviderKind(str, enum.Enum):
    PAYAMAK = "payamak"
    MOCK = "mock"
    SMTP = "smtp"
    FCM = "fcm"
    WHATSAPP_CLOUD = "whatsapp_cloud"
    TELEGRAM_BOT = "telegram_bot"
    RUBIKA_BOT = "rubika_bot"
    VOICE_GATEWAY = "voice_gateway"
    CUSTOM = "custom"


class ProviderLifecycleStatus(str, enum.Enum):
    ACTIVE = "active"
    INACTIVE = "inactive"
    DEGRADED = "degraded"
    CIRCUIT_OPEN = "circuit_open"


class CircuitState(str, enum.Enum):
    CLOSED = "closed"
    OPEN = "open"
    HALF_OPEN = "half_open"


class MessageStatus(str, enum.Enum):
    QUEUED = "queued"
    SENT = "sent"
    DELIVERED = "delivered"
    FAILED = "failed"
    EXPIRED = "expired"
    CANCELLED = "cancelled"


class TemplateStatus(str, enum.Enum):
    DRAFT = "draft"
    PENDING_APPROVAL = "pending_approval"
    APPROVED = "approved"
    REJECTED = "rejected"
    ARCHIVED = "archived"


class ContactSourceType(str, enum.Enum):
    MANUAL = "manual"
    CSV = "csv"
    CRM = "crm"
    LOYALTY = "loyalty"
    SPORTS = "sports"
    RESTAURANT = "restaurant"
    MARKETPLACE = "marketplace"
    ACCOUNTING = "accounting"
    WEBSITE = "website"
    EXTERNAL_REST = "external_rest"
    FUTURE = "future"


class QueuePriority(str, enum.Enum):
    LOW = "low"
    NORMAL = "normal"
    HIGH = "high"
    URGENT = "urgent"


class QueueItemStatus(str, enum.Enum):
    PENDING = "pending"
    PROCESSING = "processing"
    COMPLETED = "completed"
    FAILED = "failed"
    DEAD_LETTER = "dead_letter"
    CANCELLED = "cancelled"
    SCHEDULED = "scheduled"


class OTPStatus(str, enum.Enum):
    PENDING = "pending"
    VERIFIED = "verified"
    EXPIRED = "expired"
    LOCKED = "locked"
    CANCELLED = "cancelled"


class AuditAction(str, enum.Enum):
    CREATE = "create"
    UPDATE = "update"
    DELETE = "delete"
    SEND = "send"
    DELIVER = "deliver"
    FAIL = "fail"
    RETRY = "retry"
    FAILOVER = "failover"
    VERIFY = "verify"
    WEBHOOK = "webhook"
