"""Healthcare 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 LifecycleStatus(str, enum.Enum):
    DRAFT = "draft"
    ACTIVE = "active"
    INACTIVE = "inactive"
    SUSPENDED = "suspended"
    ARCHIVED = "archived"


class ProfileStatus(str, enum.Enum):
    PENDING = "pending"
    ACTIVE = "active"
    INACTIVE = "inactive"
    SUSPENDED = "suspended"
    ARCHIVED = "archived"


class ProviderKind(str, enum.Enum):
    COMMUNICATION = "communication"
    CRM = "crm"
    LOYALTY = "loyalty"
    ACCOUNTING = "accounting"
    DELIVERY = "delivery"
    STORAGE = "storage"
    AI = "ai"
    IDENTITY = "identity"
    CUSTOM = "custom"


class ProviderStatus(str, enum.Enum):
    REGISTERED = "registered"
    ACTIVE = "active"
    INACTIVE = "inactive"
    FAILED = "failed"
    RETIRED = "retired"


class AuditAction(str, enum.Enum):
    CREATE = "create"
    UPDATE = "update"
    DELETE = "delete"
    RESTORE = "restore"
    STATUS_CHANGE = "status_change"
    REGISTER = "register"
    ENABLE = "enable"
    DISABLE = "disable"


# Phase 13.1 — Appointment Engine
class DayOfWeek(str, enum.Enum):
    MONDAY = "monday"
    TUESDAY = "tuesday"
    WEDNESDAY = "wednesday"
    THURSDAY = "thursday"
    FRIDAY = "friday"
    SATURDAY = "saturday"
    SUNDAY = "sunday"


class AppointmentStatus(str, enum.Enum):
    DRAFT = "draft"
    CONFIRMED = "confirmed"
    CHECKED_IN = "checked_in"
    COMPLETED = "completed"
    CANCELLED = "cancelled"
    NO_SHOW = "no_show"


class AppointmentLifecycleAction(str, enum.Enum):
    BOOK = "book"
    CONFIRM = "confirm"
    CHECK_IN = "check_in"
    COMPLETE = "complete"
    CANCEL = "cancel"
    RESCHEDULE = "reschedule"
    MARK_NO_SHOW = "mark_no_show"


# Phase 13.2 — Doctor Panel
class VisitSessionStatus(str, enum.Enum):
    QUEUED = "queued"
    IN_PROGRESS = "in_progress"
    COMPLETED = "completed"
    DEFERRED = "deferred"
    CANCELLED = "cancelled"


class VisitSessionAction(str, enum.Enum):
    CALL_NEXT = "call_next"
    START = "start"
    FINISH = "finish"
    DEFER = "defer"


# Phase 13.3 — Clinic Management
class ClinicPolicyKind(str, enum.Enum):
    BOOKING = "booking"
    CANCELLATION = "cancellation"
    PRIVACY = "privacy"


# Phase 13.5 — Medical Record
class EncounterStatus(str, enum.Enum):
    DRAFT = "draft"
    IN_PROGRESS = "in_progress"
    FINALIZED = "finalized"


class ClinicalListKind(str, enum.Enum):
    CONDITION = "condition"
    ALLERGY = "allergy"
    MEDICATION = "medication"
    IMMUNIZATION = "immunization"


# Phase 13.6 — Pharmacy Network
class PrescriptionOrderStatus(str, enum.Enum):
    SUBMITTED = "submitted"
    ACCEPTED = "accepted"
    PREPARING = "preparing"
    READY = "ready"
    PICKED_UP = "picked_up"
    CANCELLED = "cancelled"


class PrescriptionLifecycleAction(str, enum.Enum):
    SUBMIT = "submit"
    ACCEPT = "accept"
    PREPARE = "prepare"
    MARK_READY = "mark_ready"
    COMPLETE_PICKUP = "complete_pickup"
    CANCEL = "cancel"


# Phase 13.7 — Delivery Integration
class DeliveryJobStatus(str, enum.Enum):
    QUEUED = "queued"
    PICKED_UP = "picked_up"
    IN_TRANSIT = "in_transit"
    DELIVERED = "delivered"
    FAILED = "failed"
    CANCELLED = "cancelled"
