from datetime import datetime
from typing import Any

from pydantic import BaseModel


class TaskProgress(BaseModel):
    current: int | None = None
    total: int | None = None
    percent: float | None = None
    message: str | None = None


class TaskError(BaseModel):
    type: str | None = None
    message: str | None = None


class TaskRunListItem(BaseModel):
    task_id: str
    task_name: str
    task_group: str
    task_origin: str | None = None
    status: str
    celery_state_raw: str
    progress: TaskProgress
    retries: int | None = None
    error: TaskError | None = None
    tenant_id: int
    requested_by_user: str | None = None
    started_at: datetime | None = None
    finished_at: datetime | None = None
    created_at: datetime
    updated_at: datetime


class TaskRunDetail(TaskRunListItem):
    traceback_excerpt: str | None = None
    result_summary: dict[str, Any] | None = None
    meta_payload: dict[str, Any] | None = None


class TaskRunsResponse(BaseModel):
    items: list[TaskRunListItem]
    total: int
    page: int
    page_size: int
    has_next: bool


class TaskSyncRequest(BaseModel):
    task_ids: list[str] | None = None


class TaskCatalogsResponse(BaseModel):
    task_groups: list[str]
    task_names: list[str]
    statuses: list[str]
