"""Delivery 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 ProviderKind(str, enum.Enum):
    ROUTING_ENGINE = "routing_engine"
    FLEET = "fleet"
    COURIER = "courier"
    MAPS = "maps"
    CUSTOM = "custom"


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


class RoutingEngineStatus(str, enum.Enum):
    REGISTERED = "registered"
    READY = "ready"
    ACTIVE = "active"
    DISABLED = "disabled"


class AuditAction(str, enum.Enum):
    CREATE = "create"
    UPDATE = "update"
    DELETE = "delete"
    RESTORE = "restore"
    STATUS_CHANGE = "status_change"
    REGISTER = "register"
    ENABLE = "enable"
    DISABLE = "disable"
    ACTIVATE = "activate"
    SUSPEND = "suspend"
    RESUME = "resume"
    DEACTIVATE = "deactivate"
    BLOCK = "block"
    UNBLOCK = "unblock"
    ARCHIVE = "archive"


class DriverStatus(str, enum.Enum):
    """Operational status for Delivery drivers (Torbat Driver)."""

    PENDING = "pending"
    ACTIVE = "active"
    SUSPENDED = "suspended"
    INACTIVE = "inactive"
    BLOCKED = "blocked"
    ARCHIVED = "archived"


class DriverLifecycleAction(str, enum.Enum):
    ACTIVATE = "activate"
    SUSPEND = "suspend"
    RESUME = "resume"
    DEACTIVATE = "deactivate"
    BLOCK = "block"
    UNBLOCK = "unblock"
    ARCHIVE = "archive"


class DriverCredentialKind(str, enum.Enum):
    LICENSE = "license"
    NATIONAL_ID = "national_id"
    HEALTH_CARD = "health_card"
    BACKGROUND_CHECK = "background_check"
    OTHER = "other"


class DriverDocumentKind(str, enum.Enum):
    LICENSE_SCAN = "license_scan"
    ID_SCAN = "id_scan"
    CONTRACT = "contract"
    PHOTO = "photo"
    OTHER = "other"


# Phase 10.2 — Fleet & Vehicle Types
class FleetStatus(str, enum.Enum):
    DRAFT = "draft"
    ACTIVE = "active"
    INACTIVE = "inactive"
    ARCHIVED = "archived"


class VehicleStatus(str, enum.Enum):
    AVAILABLE = "available"
    ASSIGNED = "assigned"
    MAINTENANCE = "maintenance"
    INACTIVE = "inactive"
    ARCHIVED = "archived"


class VehicleAssignmentStatus(str, enum.Enum):
    ACTIVE = "active"
    ENDED = "ended"
    CANCELLED = "cancelled"


# Phase 10.3 — Availability, Shifts & Working Zones
class DriverAvailabilityStatus(str, enum.Enum):
    ONLINE = "online"
    OFFLINE = "offline"
    BUSY = "busy"
    BREAK = "break"


class ShiftStatus(str, enum.Enum):
    DRAFT = "draft"
    SCHEDULED = "scheduled"
    ACTIVE = "active"
    COMPLETED = "completed"
    CANCELLED = "cancelled"


class ShiftAssignmentStatus(str, enum.Enum):
    ASSIGNED = "assigned"
    CHECKED_IN = "checked_in"
    COMPLETED = "completed"
    CANCELLED = "cancelled"


class WorkingZoneStatus(str, enum.Enum):
    ACTIVE = "active"
    INACTIVE = "inactive"


# Phase 10.4 — Pricing, Capabilities & Bundles
class PricingRuleStatus(str, enum.Enum):
    DRAFT = "draft"
    ACTIVE = "active"
    INACTIVE = "inactive"
    ARCHIVED = "archived"


class CapabilityStatus(str, enum.Enum):
    ACTIVE = "active"
    INACTIVE = "inactive"


class CapabilityBundleStatus(str, enum.Enum):
    DRAFT = "draft"
    ACTIVE = "active"
    INACTIVE = "inactive"
    ARCHIVED = "archived"


# Phase 10.5 — Dispatch Engine
class DispatchJobStatus(str, enum.Enum):
    PENDING = "pending"
    ASSIGNED = "assigned"
    IN_PROGRESS = "in_progress"
    COMPLETED = "completed"
    CANCELLED = "cancelled"
    FAILED = "failed"


class DispatchJobAction(str, enum.Enum):
    ASSIGN = "assign"
    START = "start"
    COMPLETE = "complete"
    CANCEL = "cancel"
    FAIL = "fail"


class JobAssignmentStatus(str, enum.Enum):
    PENDING = "pending"
    ACCEPTED = "accepted"
    REJECTED = "rejected"
    ACTIVE = "active"
    COMPLETED = "completed"
    CANCELLED = "cancelled"


# Phase 10.6 — Routing & Optimization
class RoutePlanStatus(str, enum.Enum):
    DRAFT = "draft"
    PLANNED = "planned"
    ACTIVE = "active"
    COMPLETED = "completed"
    CANCELLED = "cancelled"


class RouteStopStatus(str, enum.Enum):
    PENDING = "pending"
    ARRIVED = "arrived"
    COMPLETED = "completed"
    SKIPPED = "skipped"


class OptimizationRunStatus(str, enum.Enum):
    PENDING = "pending"
    RUNNING = "running"
    COMPLETED = "completed"
    FAILED = "failed"


# Phase 10.7 — Tracking & Proof of Delivery
class TrackingSessionStatus(str, enum.Enum):
    ACTIVE = "active"
    PAUSED = "paused"
    COMPLETED = "completed"
    CANCELLED = "cancelled"


class ProofOfDeliveryStatus(str, enum.Enum):
    PENDING = "pending"
    CAPTURED = "captured"
    VERIFIED = "verified"
    REJECTED = "rejected"


class CustomerTrackingTokenStatus(str, enum.Enum):
    ACTIVE = "active"
    EXPIRED = "expired"
    REVOKED = "revoked"


# Phase 10.8 — Settlement
class SettlementIntentStatus(str, enum.Enum):
    DRAFT = "draft"
    PENDING = "pending"
    SUBMITTED = "submitted"
    SETTLED = "settled"
    FAILED = "failed"
    CANCELLED = "cancelled"


class SettlementLineKind(str, enum.Enum):
    DELIVERY_FEE = "delivery_fee"
    TIP = "tip"
    SURCHARGE = "surcharge"
    ADJUSTMENT = "adjustment"
    OTHER = "other"
