from __future__ import annotations

from datetime import date
from typing import Any, Literal

from pydantic import BaseModel, Field


class BackfillInput(BaseModel):
    start_date: date
    end_date: date


class EventRevisionInput(BaseModel):
    event_type: Literal["limit_up", "broken", "limit_down"]
    reason: str = Field(min_length=1, max_length=200)
    first_time: str = Field(default="", max_length=5)
    last_time: str = Field(default="", max_length=5)
    open_times: int | None = Field(default=None, ge=0)


class OperationResult(BaseModel):
    model_config = {"extra": "allow"}


class JobRunResponse(BaseModel):
    id: int
    kind: str
    run_key: str
    requested_date: str
    trigger: str
    status: Literal["running", "completed", "failed"]
    attempt: int
    started_at: str
    finished_at: str | None
    duration_ms: int | None
    coverage: float | None
    source_set: list[str]
    output_version: str
    payload: dict[str, Any]
    error_code: str
    error_message: str


class RuntimeStatusResponse(BaseModel):
    state: Literal["idle", "running", "ready", "degraded"]
    message: str
    trade_date: str | None = None
    observed_at: str | None = None
