"""客户端运行日志批量上报 schema。

批级公共字段(device_id/user_id/app_ver/platform/sent_at)发一次;logs 为原始 dict 列表,
**不强类型**——尽力而为的日志链路,单条内容异常不该让整批 422。每条的「白名单键 + data
兜底」拆分在写入层 [app.core.client_log] 做(见 spec §4/§5)。
"""
from __future__ import annotations

import os
from typing import Any

from pydantic import BaseModel, Field

# 单批条数上限。注意:这是**导入期**常量(Pydantic Field(max_length=) 在类定义时求值),
# 改它需重启进程;要运行期可调的上限用 APPLOG_MAX_BODY_BYTES(端点里 call-time 读)。
_MAX_BATCH = int(os.getenv("APPLOG_MAX_BATCH", "500"))


class AppLogBatchIn(BaseModel):
    device_id: str = Field(max_length=64)
    user_id: int | None = None
    app_ver: str | None = Field(default=None, max_length=32)
    platform: str | None = Field(default=None, max_length=16)
    sent_at: int | None = None
    logs: list[dict[str, Any]] = Field(min_length=1, max_length=_MAX_BATCH)


class AppLogIngestOut(BaseModel):
    ok: bool = True
    received: int
    dropped: int = 0
