from datetime import datetime from typing import Annotated, Literal from uuid import UUID from pydantic import AwareDatetime, BaseModel, ConfigDict, Field from messenger.attachments.domain.jobs import Job from messenger.attachments.domain.models import Attachment from messenger.attachments.domain.variants import Variant type VariantName = Literal["original", "thumbnail", "crop", "optimized"] class VariantOut(BaseModel): name: str content_type: str size_bytes: int width: int height: int sha256: str @classmethod def of(cls, variant: Variant) -> VariantOut: return cls( name=variant.name, content_type=variant.content_type, size_bytes=variant.size_bytes, width=variant.width, height=variant.height, sha256=variant.sha256, ) class AttachmentOut(BaseModel): id: UUID chat_id: UUID owner_id: UUID filename: str content_type: str size_bytes: int sha256: str status: str variants: list[VariantOut] error_code: str | None created_at: datetime updated_at: datetime @classmethod def of(cls, attachment: Attachment) -> AttachmentOut: return cls( id=attachment.id, chat_id=attachment.chat_id, owner_id=attachment.owner_id, filename=attachment.filename.value, content_type=attachment.content_type, size_bytes=attachment.size_bytes, sha256=attachment.sha256, status=attachment.status.value, variants=[VariantOut.of(variant) for variant in attachment.variants], error_code=attachment.error_code, created_at=attachment.created_at, updated_at=attachment.updated_at, ) class JobMessage(BaseModel): model_config = ConfigDict(extra="forbid", frozen=True) schema_version: Literal[1] job_id: UUID type: Literal["attachment.process.v1"] attachment_id: UUID pipeline_version: Annotated[int, Field(ge=1)] attempt: Annotated[int, Field(ge=1)] created_at: AwareDatetime traceparent: Annotated[ str | None, Field(pattern=r"^00-[0-9a-f]{32}-[0-9a-f]{16}-[0-9a-f]{2}$") ] = None tracestate: Annotated[str | None, Field(max_length=512)] = None def job(self) -> Job: return Job( self.job_id, self.attachment_id, self.pipeline_version, self.attempt, self.created_at )