from datetime import datetime
from enum import Enum
from typing import Optional, List
from pydantic import BaseModel, Field

class ThreadType(str, Enum):
    TEXT_CHAT = "TEXT_CHAT"
    CALLBACK_REQUEST = "CALLBACK_REQUEST"
    APPOINTMENT = "APPOINTMENT"

class SenderRole(str, Enum):
    CITIZEN = "CITIZEN"
    OFFICIAL = "OFFICIAL"
    SYSTEM = "SYSTEM"

class DocumentMetadata(BaseModel):
    authorityId: str = Field(..., max_length=50)
    referenceNumber: str = Field(..., max_length=50)
    docType: str = Field(..., max_length=50)
    issuedAt: datetime

class DocumentCreateRequest(BaseModel):
    metadata: DocumentMetadata
    encryptedPayload: str  # base64-encoded opaque blob

class DocumentCreatedResponse(BaseModel):
    id: str
    status: str
    assignedUnit: Optional[str] = None

class ThreadCreateRequest(BaseModel):
    type: ThreadType
    initialMessage: Optional[str] = None
    preferredTimeSlot: Optional[datetime] = None

class ThreadCreatedResponse(BaseModel):
    threadId: str
    status: str
    estimatedWaitTime: Optional[str] = None

class MessageCreateRequest(BaseModel):
    content: str  # encrypted message string

class MessageDto(BaseModel):
    id: str
    senderRole: SenderRole
    content: str
    timestamp: datetime

class MessageListResponse(BaseModel):
    data: List[MessageDto]
    paging: dict

class ExportRequest(BaseModel):
    caseId: str
    targetSystem: str
    includeAttachments: bool = True

class ExportJobStatus(str, Enum):
    QUEUED = "QUEUED"
    RUNNING = "RUNNING"
    COMPLETED = "COMPLETED"
    FAILED = "FAILED"

class ExportCreatedResponse(BaseModel):
    jobId: str
    status: ExportJobStatus

class ExportStatusResponse(BaseModel):
    jobId: str
    status: ExportJobStatus
    statusUrl: Optional[str] = None
