"""Public API request/response schemas (TZ 2.8.5)."""

from __future__ import annotations

from typing import Any, Literal

from ninja import Field, Schema


class OutputIn(Schema):
    format: Literal["pdf", "png", "jpg"] = "pdf"
    dpi: int = Field(150, ge=36, le=600)
    quality: int = Field(85, ge=1, le=100)
    split: Literal["per_sheet", "zip", "first"] | None = None
    pages: str | None = Field(None, description='Page ranges, e.g. "1-3,5"')
    background: str | None = Field(None, pattern=r"^#[0-9A-Fa-f]{6}$")
    pdfa: Literal["1b", "2b"] | None = None
    store: bool = True
    ttl_hours: int | None = Field(None, ge=1, le=720)
    filename: str | None = Field(None, max_length=200)
    locale: str | None = Field(None, max_length=16, description="Formatting locale; default data.lang, then project")


class RenderIn(Schema):
    version: int | Literal["current"] = "current"
    lang: str | None = Field(
        None,
        max_length=16,
        description="Language: picks the group's default template for it (key = group key); default data.lang",
    )
    mode: Literal["sync", "async", "auto"] = "auto"
    data: dict[str, Any] = Field(default_factory=dict)
    output: OutputIn = Field(default_factory=OutputIn)
    validation: Literal["strict", "lenient"] = "strict"
    external_ref: str | None = Field(None, max_length=255)
    webhook_url: str | None = Field(None, max_length=512)
    deterministic: bool = False


class ValidateIn(Schema):
    version: int | Literal["current"] = "current"
    lang: str | None = Field(None, max_length=16, description="Language, as in render; default data.lang")
    data: dict[str, Any] = Field(default_factory=dict)
    validation: Literal["strict", "lenient"] = "strict"


class PreviewIn(Schema):
    version: int | Literal["current"] = "current"
    lang: str | None = Field(None, max_length=16, description="Language, as in render; default data.lang")
    data: dict[str, Any] = Field(default_factory=dict)
    pages: str = "1"
    dpi: int = Field(96, ge=36, le=150)
    format: Literal["png", "jpg", "pdf"] = "png"


class FileOut(Schema):
    id: str
    kind: str
    filename: str
    mime: str
    size_bytes: int
    sha256: str
    page: int | None = None
    width_px: int | None = None
    height_px: int | None = None
    url: str | None = None
    expires_at: str


class JobError(Schema):
    code: str | None
    message: str


class JobTemplateRef(Schema):
    key: str
    version: int | None


class JobOut(Schema):
    id: str
    status: Literal["queued", "running", "succeeded", "failed", "canceled", "expired"]
    mode: str
    kind: str
    attempts: int
    template: JobTemplateRef
    pages: int | None = None
    files: list[FileOut] = []
    warnings: list[dict[str, Any]] = []
    timings: dict[str, Any] = {}
    external_ref: str | None = None
    status_url: str
    created_at: str
    queued_at: str | None = None
    started_at: str | None = None
    finished_at: str | None = None
    error: JobError | None = None


class JobEnvelope(Schema):
    job: JobOut


class JobList(Schema):
    data: list[JobOut]
    next_cursor: str | None


class ErrorItem(Schema):
    code: str
    message: str
    details: list[dict[str, Any]] = []
    request_id: str | None = None


class ErrorOut(Schema):
    error: ErrorItem


class EditUser(Schema):
    id: str = Field(..., max_length=191)
    name: str = Field(..., max_length=255)
    email: str | None = None
    avatar_url: str | None = None


class EditSessionIn(Schema):
    user: EditUser
    role: Literal["viewer", "editor", "admin"] = "editor"
    permissions: dict[str, bool] = Field(default_factory=dict)
    templates: list[str] | None = None
    open: dict[str, Any] | None = None
    locale: Literal["ru", "en"] | None = None
    return_url: str | None = None
    client_session_ref: str | None = None
    ttl_minutes: int = Field(480, ge=5, le=1440)


class EditSessionOut(Schema):
    id: str
    url: str
    sso_url: str
    handoff_expires_at: str
    expires_at: str


class EditSessionEnvelope(Schema):
    session: EditSessionOut


class RevokeUserIn(Schema):
    external_user_id: str


class WebhookConfigIn(Schema):
    url: str | None = Field(None, max_length=512)
    rotate_secret: bool = False
