import uuid
from datetime import datetime
from typing import Optional
from uuid import UUID

from pydantic import Field
from tacoq.core.models.avro_serializable_base_model import (
    AvroSerializableBaseModel,
    avro_schema_path,
)

from tacoq.core.models.task import TaskOutput


@avro_schema_path("schemas/avro/task_completed_update.json")
class TaskCompletedUpdate(AvroSerializableBaseModel):
    """Update of a task being completed by the worker."""

    id: UUID = Field(default_factory=uuid.uuid4)
    """The unique ID of the task. Generated by the client so that it can be 
    communicated to the relay and the workers directly."""

    completed_at: datetime = Field(default_factory=lambda: datetime.now())
    """ The time the task was completed at. """

    output_data: Optional[TaskOutput] = Field()
    """ The data output by the task."""

    is_error: int = Field()
    """ Whether the task failed. Used primarly for the dead letter queue."""
